0

目前,我有一个按钮,可以将值发送到另一个表单并在标签中显示结果。问题是,我有 20 个标记为 a 到 w 的按钮需要编码,我对如何从多个按钮传递值感到困惑。它会是传递给表单的案例陈述吗?我是 VB.Net 的新用户,并且仍在寻找我的方式,因此我们将不胜感激地收到任何帮助。我已经包含了第一个按钮“A”的代码示例。谢谢

主界面

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

框架架

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
4

1 回答 1

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
于 2013-10-26T09:38:52.100 回答