1

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

1 Answer 1

3

Its simple, the below will help,

AddHandler CommandColor(i).Click, AddressOf SubName

Below is the revisions required in your code,

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).Name = "btn" & (i+1)
        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
        AddHandler CommandColor(i).Click, AddressOf Button_click


        Me.Controls.Add(CommandColor(i))
    Next
End Sub

Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim btn As Button = DirectCast(sender, Button)
      Select btn.Name
           Case "btn1"
                 btn.BackColor = Color.Red
           Case "btn2"
                 btn.BackColor = Color.Green
           Case "btn3"
                 btn.BackColor = Color.Yellow
           Else
                 btn.BackColor = Color.Silver
      End Select
End Sub
End Class
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. It looks you add a brand new button on form1. However, my goal is --- There 3 button array on form1, and …. .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.
I've edited the answer, you need to assign the buttons a identifier by setting the name property, the same can be used in the click event to differentiate the buttons and set colors accordingly.
@AnandSeniyar or you could just assign the index to the Tag property and use that
it works! I have learned much today. thank you veery much, Seniyar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.