我想使用For循环
在表单上创建64个按钮For i = 1 To 64
Dim WithEvents B(i) As New Button
Next
但是这段代码对我不起作用。任何想法或方法一次创建几个按钮?!
答案 0 :(得分:0)
这是一个简单的例子:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 64
Dim btn As New Button
btn.Text = "Button #" & i
' wire up the Click() event of the button:
AddHandler btn.Click, AddressOf btn_Click
' add the button to some kind of container...
' I 'm using a FlowLayoutPanel so I don't have to explicitly set a Location()
FlowLayoutPanel1.Controls.Add(btn)
Next
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
' cast the "sender" parameter to the source of the event:
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show(btn.Text & " clicked.")
End Sub