vb2010从多个按钮获取名称值

时间:2013-10-26 09:14:32

标签: vb.net visual-studio-2010

目前,我有一个按钮,可以将值发送到另一个表单并在标签中显示结果。问题是,我有20个标记为a到w的按钮需要编码,我很难过如何从多个按钮传递值。是否会以传递给的形式发表案例陈述?我是VB.Net的新用户,仍然找到了我的方式,所以任何帮助都会感激不尽。我已经为第一个按钮'A'添加了代码示例。感谢

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

编辑:新项目

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

1 个答案:

答案 0 :(得分:1)

这里有一个示例代码,希望能帮助您获得更清晰的想法:

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