Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Arrays. All the components in the Visual Basic toolbox are referred to as controls A control array is a group of controls of the same type that.

Similar presentations


Presentation on theme: "Control Arrays. All the components in the Visual Basic toolbox are referred to as controls A control array is a group of controls of the same type that."— Presentation transcript:

1 Control Arrays

2 All the components in the Visual Basic toolbox are referred to as controls A control array is a group of controls of the same type that have the same name and share the same set of event procedures. Each control in a control array is referred to by the array's name and the control's index.

3

4 Private Sub Form_Load() Dim depNum As Integer For depNum = 0 To 4 lblDepart(depNum).Caption = "Department" & Str(depNum + 1) Next depNum End Sub Private Sub cmdCompute_Click() Dim depNum As Integer, sales As Single sales = 0 For depNum = 0 To 4 sales = sales + Val(txtSales(depNum).Text) Next depNum picTotal.Cls picTotal.Print "Total sales were " & FormatCurrency(sales) End Sub

5 Two Ways to Create a Control Array 1.If the controls are not on the form, place the first control on the form and set its properties appropriately. Copy the control to the clipboard, and paste the appropriate number of controls on the form. When asked if you want to create a control array, click the Yes button. 2.If the controls are already on the form, simply give each the same name. When asked if you want to create a control array, click the Yes button

6 Control Array Event Procedures All elements of the control arrays share the same event procedures. All event procedures for a control array have the additional parameter Index As Integer Example: Private Sub txtBox_GotFocus(Index as Integer) Select Case Index Case 0 action when txtBox(0) gets the focus Case 1 action when txtBox(1) gets the focus. End Select End Sub

7 Private Sub optChoice_Click(Index As Integer) Select Case Index Case 0 lblMessage.Caption = “You chose the first option button” Case 1 lblMessage.Caption = “You chose the second option button” Case 2 lblMessage.Caption = “You chose the third option button” Case 3 lblMessage.Caption = “You chose the fourth option button” Case 4 lblMessage.Caption = “You chose the fifth option button” End Select End Sub


Download ppt "Control Arrays. All the components in the Visual Basic toolbox are referred to as controls A control array is a group of controls of the same type that."

Similar presentations


Ads by Google