For i = 1 To numRowDraw
For j = 1 To numColumnDraw
' สร้างปุ่ม
btnArray(i) = New System.Windows.Forms.Button
btnArray(i).Parent = pnel
btnArray(i).Text = ""
btnArray(i).Location = New System.Drawing.Point(x, y)
btnArray(i).Size = New System.Drawing.Size(3 * 10, 13 * 10)
btnArray(i).BackColor = Color.White
x += 3 * 10
AddHandler btnArray(i).Click, AddressOf Me.HandleDynamicButtonClick
Next
x = 0
y += 13 * 10
Next
Code (VB.NET)
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = sender
If btn.BackColor = Color.White Then
btn.BackColor = Color.Red
ElseIf btn.BackColor = Color.Red Then
btn.BackColor = Color.White
End If
End Sub