-4

如何在 VB.NET 中创建一组按钮?

 cmdButton(0)
 cmdButton(1)
 cmdButoon(2)
4

1 回答 1

1

你可以像这样创建一个数组

Dim btnCommand(2) As Button

但是您必须将它添加到您的表单中并添加一个点击处理程序才能使它做任何有用的事情:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim y As Integer = 25
    Dim btnCommand(2) As Button
    For i As Integer = 0 To btnCommand.Length - 1
        btnCommand(i) = New Button
        Me.Controls.Add(btnCommand(i))
        With btnCommand(i)
            .Top = y
            .Tag = i
            .Text = "Button " + i.ToString
        End With
        y += 25
        AddHandler btnCommand(i).Click, AddressOf ButtonArray_Click
    Next
End Sub

Private Sub ButtonArray_Click(sender As System.Object, e As System.EventArgs)
    Dim btn As Button = sender
    MsgBox("Button " + btn.Tag.ToString + " was clicked")
End Sub 
于 2012-07-15T20:32:03.560 回答