2013-10-26 37 views
0

此刻,我有一個按鈕,它將值發送到另一個窗體並將結果顯示在標籤中。問題是,我有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 
+0

您之前提出過這個問題,我發現至少有一個答案給出了一個代碼,允許您執行顯然需要的內容。也許有誤解(要麼你不完全理解答案,要麼我(或答覆者)不理解你想要的)。讓我試着澄清一下:什麼是btnA_MouseDown?與所有按鈕關聯的點擊事件方法? – varocarbas

+0

@varocarbas我問過關於多個按鈕的問題?這是僅在當前按鈕「A」處關聯的點擊事件。謝謝 – user1532468

+0

那麼也許這就是誤解:你最後的問題/答案適用於一個按鈕和1000個按鈕。您可以將btnA_MouseDown與您想要的按鈕關聯起來,並檢索相應的名稱(不確定爲什麼您現在將其硬編碼爲frmRacks.buttonName =「A」) – varocarbas

回答

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 
+0

謝謝你。但是,我有一個問題。在閱讀你的代碼後,我是否正確地思考:你的form1_load將會是我的frmMain,而Mousedown的sub將是我的frm \ rack來捕獲事件?謝謝 – user1532468

+0

@ user1532468此代碼旨在提供您應該瞭解的一般方法。它將按鈕添加到主窗體中(例如,您可以用任何窗體替換它,例如您提到的窗體);在定義按鈕時,它會關聯相應的事件(在這種情況下爲MouseDown,但您可以對任何其他事件執行相同的操作);取而代之的是創建多個按鈕的方法,它對所有的方法都使用相同的方法,並利用當前按鈕的sender參數(並檢索您想要的.Name屬性)。所有這些想法都清楚嗎? – varocarbas

+0

不是。我在我的問題中說過,我是一名新用戶,在閱讀和教程兩週後纔開始掌握實際編碼。謝謝 – user1532468