Control Array

 

Today I answered another  VB question under the Yahoo! Answer section and I wish to share it with you here. It goes like this "How do I make an array of option buttons so that they all do different stuff?what i mean is : I put 1 option button into a frame. I then copied it and got prompted with: "do you want to make a control array"  I said yes then I put 9 different buttons into the frame and they are option1(0) option1(1) option1(2) option1(3) .... option1(8) now when I double click one of them it takes me to the code but no matter which one I click, it brings me to the same sub within the code... how do  I make it so each option button does something different?." . Below is the solution for the above question:

 

Private Sub Command1_Click()
For Index = 0 To 6
Label1(Index).BackColor = vbWhite
Next
End Sub

Private Sub Option1_Click(Index As Integer)

Select Case Index
Case 0
Label1(0).BackColor = vbRed
Case 1
Label1(1).BackColor = &H80FF&
Case 2
Label1(2).BackColor = vbYellow
Case 3
Label1(3).BackColor = vbGreen
Case 4
Label1(4).BackColor = vbBlue
Case 5
Label1(5).BackColor = &H800000
Case 6
Label1(6).BackColor = &H800080

End Select

End Sub
 

Explanations

 The key point in this program is to use index of the control array. Here I inserted a control array of seven optional buttons.

Each option button in the array is identified by the index, starting with 0 and end with 7.

Then I used the Select Case....End Select to make each option button performs different job, that is to change the background color of the labels to different colors.

The labels are also a control array differentiated by their indexes.

The end result is a rainbow.

 

 

 

 

 

 [Back to VBToday]