user1532468
user1532468

Reputation: 1753

vb2010 getting name values from multiple buttons

At the moment, I have a button that sends a value to another form and displays result in a label. The problem is, I have 20 buttons that are labeled a to w that need to be coded and I am stumped as to how I can pass values from multiple buttons. Would it be a case statement in the form being passed to? I am a new user to VB.Net and still finding my way so any help would be gratefully received. I have included code sample for the first button 'A'. Thanks

frmMain

Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
        If (e.Button = MouseButtons.Right) Then
            'Dim curButton As Button = DirectCast(sender, Button)
            'frmRacks.buttonName = curButton.Name 'Dynamic alternative to frmRacks.buttonName = "A"
            frmRacks.buttonName = "A"
            frmRacks.Show()
        ElseIf (e.Button = MouseButtons.Left) Then
            MessageBox.Show("To be coded")
        End If
    End Sub

frmRacks

Public Class frmRacks
    Public buttonName As String
    Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblRacks.Text = buttonName

    End Sub

EDIT: New project

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim button1 As Button = New Button
        Dim button2 As Button = New Button
        Dim button3 As Button = New Button

        With button1
            .Name = "button1"
            .Left = 0
            AddHandler .MouseDown, AddressOf btn_MouseDown
            'Add remaining properties for button1
        End With

        With button2
            .Name = "button2"
            .Left = 100
            AddHandler .MouseDown, AddressOf btn_MouseDown
            'Add remaining properties for button2
        End With

        With button3
            .Name = "button3"
            .Left = 200
            AddHandler .MouseDown, AddressOf btn_MouseDown
            'Add remaining properties for button3
        End With

        Me.Controls.Add(button1)
        Me.Controls.Add(button2)
        Me.Controls.Add(button3)

    End Sub

    Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        Dim curButton As Button = DirectCast(sender, Button)
        Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
        Form2.buttonName = curButtonName
        Form2.Show()
        'MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
    End Sub
End Class

Public Class Form2
    Public buttonName As String
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lblRacks.Text = buttonName
    End Sub
End Class

Upvotes: 0

Views: 762

Answers (1)

user2480047
user2480047

Reputation:

Here you have a sample code which hopefully will help you to get clearer ideas:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim button1 As Button = New Button
    Dim button2 As Button = New Button
    Dim button3 As Button = New Button

    With button1
        .Name = "button1"
        .Left = 0
        AddHandler .MouseDown, AddressOf btn_MouseDown
        'Add remaining properties for button1
    End With

    With button2
        .Name = "button2"
        .Left = 100
        AddHandler .MouseDown, AddressOf btn_MouseDown
        'Add remaining properties for button2
    End With

    With button3
        .Name = "button3"
        .Left = 200
        AddHandler .MouseDown, AddressOf btn_MouseDown
        'Add remaining properties for button3
    End With

    Me.Controls.Add(button1)
    Me.Controls.Add(button2)
    Me.Controls.Add(button3)

End Sub

Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Dim curButton As Button = DirectCast(sender, Button)

    Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked

    MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub

Upvotes: 1

Related Questions