2013-02-11 44 views
-1

我在VB2010中工作,我想我可以在代碼中創建一個按鈕數組;然而,我很努力地單獨引用創建的按鈕來編寫它們的點擊事件,以便它們在運行時工作。在vb2010代碼中動態創建按鈕

任何幫助將不勝感激。我對vb programmimg相當陌生,所以對我來說很簡單!

回答

0

在其中一個按鈕點擊事件插入類似button2.click這將執行相同的操作。

或者你可能需要考慮addHandler操作.....

1

這給一試:

' However many buttons you want 
Dim numButtons As Integer = 5 
Dim ButtonArray(numButtons) as Button 
Dim i As Integer = 0 
For Each b As Button in ButtonArray 
    b = new button 
    AddHandler b.Click, AddressOf Me.ButtonsClick 
    b.Tag = "b" & i 
    ' You can also set things like button text in here. 
    i += 1 
Next 


Private Sub ButtonsClick(sender As Object, e As System.EventArgs) 
    ' sender is the button that has been clicked. You can 
    ' do what you'd like with it, including cast it as a Button. 
    Dim currButton As Button = CType (sender, Button) 
    Select Case currButton.Tag 
     Case "b0": 
      ' This is the first button in the array. Do things! 
     Case "b1": 
      ' This is the second button in the array. Do things! 
     Case "b2": 
      ' Notice a pattern? 

     '... 

    End Select 
End Sub 
+0

+1,但如果你告訴他如何在第二個方法,你會得到更多的選票描述一個按鈕點擊事件,處理Button1.Click,Button2.Click – 2013-02-11 22:25:26