在vb2010中动态创建代码中的按钮

时间:2013-02-11 21:08:12

标签: vb.net button addhandler

我在VB2010工作,我想我可以在代码中创建一个按钮数组;但是,我很难单独引用创建的按钮来编写他们的点击事件,以便它们在运行时工作。

非常感谢任何帮助。我是vb programmimg的新手,所以对我很轻松!!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在其中一个按钮点击事件中插入类似button2.click的内容,它将执行相同的操作。

或者您可能需要查看addHandler .....