XTreme Logo

If you appreciate these tips, please consider a small donation:

General Tip - Intermediate

Using Control Arrays To Your Advantage
Applies to: VB4 and VB5, should also work in other versions
Courtesy of Steve Richardson

It makes sense to use control arrays to manipulate groupings of similiar controls.

The control array will allow you to loop through groups of similiar controls and perform a desired action. Control Arrays can be especially useful on questionnaire type forms for capturing groups of related data.

The example shown below makes use of two control arrays Text1(Index) representing the text boxes in Column A and Text2(Index) representing the text boxes in Column B. The command buttons will perform actions (Clear, Enable/Disable, Load Text) on the column of text boxes being specified by the selected radio button. To perform the desired action on either array simply loop on the index value of that array.

Image of form with 3 buttons, 2 arrays of 3 textboxes, and 2 option buttons.

'Clear

Private Sub Command1_Click()
   
If Option1 = True Then
       
For Index = 0 To 2 'Column A Array

           Text1(Index).Text = ""
'Action
       
Next
   
Else
       
For Index = 0 To 2 'Column B Array

           Text2(Index).Text = ""
'Action
       
Next
   
End If
End Sub

'Enable / Disable

Private Sub Command2_Click()
    If Option1 = True Then
       
For Index = 0 To 2 'Column A Array
            Text1(Index).Enabled = Not Text1(Index).Enabled 'Action
        Next
   
Else
       
For Index = 0 To 2 'Column B Array
            Text2(Index).Enabled = Not Text2(Index).Enabled 'Action
        Next
   
End If
End Sub

'Load Text

Private Sub Command3_Click()
    Dim strLoad As String
    If Option1 = True Then
        For Index = 0 To 2 'Column A Array
            strLoad = "Text1(" & Index & ")" 'Action
            Text1(Index).Text = strLoad
        Next
   
Else
       
For Index = 0 To 2 'Column B Array
            strLoad = "Text2(" & Index & ")" 'Action
            Text2(Index).Text = strLoad
        Next
   
End If
End Sub


[ Back To The Top ]

Contact: web@xtremecomp
All contents copyright XTreme Computing unless noted otherwise.