Sie sind auf Seite 1von 4

Control Arrays in Visual Basic 6.

0
5.6 Control Arrays 5.6.1 Setting up the control Array 5.6.2 To remove a control Array 5.6.3 To add and delete controls at run time
A control array is a group of controls that share the same name type and the same event procedures. Adding controls with control arrays uses fewer resources than adding multiple control of same type at design time. A control array can be created only at design time, and at the very minimum at least one control must belong to it. You create a control array following one of these three methods:

You create a control and then assign a numeric, non-negative value to its Index property; you have thus created a control array with just one element. You create two controls of the same class and assign them an identical Name property. Visual Basic shows a dialog box warning you that there's already a control with that name and asks whether you want to create a control array. Click on the Yes button.

Sharing Event Handlers The following example demonstrates sharing the Change event handler (TextChanged in Visual Basic 2005) for a group of three TextBox controls. In Visual Basic 2005, the Handles clause of the event handler specifies which control the event will handle. The event handler returns a generic Object, so it must be cast to the specific object type (in this case, TextBox) that you want to handle using the DirectCast method.

Private Sub Text1_Change (Index As Integer) Select Case Index Case 0 MsgBox("The text in the first TextBox has changed") Case 1 MsgBox("The text in the second TextBox has changed") Case 2 MsgBox("The text in the third TextBox has changed") End Select End Sub To Clear the Content of Control Array of TextBoxes ' Visual Basic 6.0 Private Sub ClearText() For i = 0 To Text1().UBound Text1(i).Text = "" Next End Sub

Adding Controls at Run Time The following example demonstrates adding a text-box control to a form at run time. In Visual Basic 6.0, events for the new TextBox were automatically handled by the control array. 'Visual Basic 6.0 Private Sub AddControl()

'Add a TextBox as the second element of a control array. Load Text1(1) 'Set the location below the first TextBox. Text1(1).Move Text1(0).Left, Text1(0).Top + 500 'Make the new TextBox visible Text1(1).Visible = True

To add controls at runtime, there must already be at least one instance of the control on the form, and it must be part of a control array. To create a control array containing only a single control, add the control to the form and then set its Index property to 0. This control doesn't have to be visible. When the program runs, you add additional controls with the Load statement: Load ControlName (Index). ControlName is the Name property of the control array you created and Index is the Index property of the new control. You should start at 1 because Index 0 is already taken by the control you placed on the form at design time. Each control that you add in this way initially has the same properties as the original control. You'll have to change at least the Top and Left properties to prevent all the added controls from displaying at the same position onscreen. This form has a label named label1 (index-0) and a command button named commandbutton1 [CODE]

Private Sub Command1_Click() Label1(0).Caption = "i am zero" For I = 1 To 10 Load Label1(I) Label1(I).Caption = I Label1(I).Top = 700 * I Label1(I).Visible = True Next I End Sub Private Sub Label1_Click(Index As Integer) MsgBox ("i am from label " & Index) End Sub

Das könnte Ihnen auch gefallen