0

現時点では、値を別のフォームに送信し、結果をラベルに表示するボタンがあります。問題は、コーディングが必要な a から w までのラベルが付いた 20 個のボタンがあり、複数のボタンから値を渡す方法に困惑していることです。渡されるフォームのケースステートメントでしょうか?私は VB.Net の新しいユーザーであり、まだ自分の道を見つけているので、どんな助けも感謝して受け取ります。最初のボタン「A」のコード サンプルを含めました。ありがとう

fromMain

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

frmラック

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 に答える