Public Class Form1
Dim myButton() As Button
Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) '這邊的部份使用其他物件的click事件,並把後面Handle...給刪除
Dim thisButton As Button = sender
MsgBox("你按了第" & Val(Strings.Right(thisButton.Name, 2)) & "個按鈕!") '右邊兩個字即為按鈕的編號
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 14
ReDim Preserve myButton(i) '新增陣列長度
myButton(i) = New Button '產生按鈕類別
myButton(i).Name = "myButton" & Format(i, "00") '
myButton(i).Size = New Size(30, 30) '設定大小
myButton(i).Left = (i Mod 5) * 35 + 10 '按鈕由左到右排列,五個則換一行
myButton(i).Top = (i \ 5) * 35 + 10
myButton(i).Text = Format(i, "00")
Me.Controls.Add(myButton(i)) '將新增的按鈕放到表單上
AddHandler myButton(i).Click, AddressOf myButton_Click '指派事件到這個按鈕
Next
End Sub
End Class