此刻,我有一個按鈕,它將值發送到另一個窗體並將結果顯示在標籤中。問題是,我有20個按鈕被標記爲w,需要編碼,我很難理解如何從多個按鈕傳遞值。這是傳遞給表單的案例陳述嗎?我是VB.Net的新用戶,並且仍然在尋找我的方式,所以我會非常感激地接受任何幫助。我已經包含第一個按鈕'A'的代碼示例。由於vb2010從多個按鈕獲取名稱值
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
您之前提出過這個問題,我發現至少有一個答案給出了一個代碼,允許您執行顯然需要的內容。也許有誤解(要麼你不完全理解答案,要麼我(或答覆者)不理解你想要的)。讓我試着澄清一下:什麼是btnA_MouseDown?與所有按鈕關聯的點擊事件方法? – varocarbas
@varocarbas我問過關於多個按鈕的問題?這是僅在當前按鈕「A」處關聯的點擊事件。謝謝 – user1532468
那麼也許這就是誤解:你最後的問題/答案適用於一個按鈕和1000個按鈕。您可以將btnA_MouseDown與您想要的按鈕關聯起來,並檢索相應的名稱(不確定爲什麼您現在將其硬編碼爲frmRacks.buttonName =「A」) – varocarbas