I have used VB6 for many years. Recently, I am trying to use vb.net.
the following are button array and codes in VB6 and they work well.
Private Sub CommandColor_Click(Index As Integer)
If Index = 0 Then CommandColor(Index).BackColor = vbRed
If Index = 1 Then CommandColor(Index).BackColor = vbGreen
If Index = 2 Then CommandColor(Index).BackColor = vbYellow
End Sub
- When I click
CommandColor(0), it changes color to red. - When I click
CommandColor(1), it changes color to green. - When I click
CommandColor(2), it changes color to yellow.
Now, I am trying to do the same things in VB2010. The following codes are what I have finished in establishing the dynamic control array. However, I don’t know how to code “the Sub of button_Click” in dynamic button array. Anyone can help me? Thanks in advance.
Public Class Form1
Dim CommandColor(2) As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To 2
CommandColor(i) = New Button
CommandColor(i).Text = "CommandColor" + Trim(Str(i))
CommandColor(i).ForeColor = Color.Black
CommandColor(i).Left = 50
CommandColor(i).Width = 250
CommandColor(i).Height = 50
CommandColor(i).Top = 50 + 66 * i
CommandColor(i).TextAlign = System.Drawing.ContentAlignment.MiddleCenter
CommandColor(i).Font = New Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular)
CommandColor(i).BackColor = Color.Silver
Me.Controls.Add(CommandColor(i))
Next
End Sub
End Class