Share via


Control Arrays for Visual Basic 6.0 Users

Although control arrays are no longer supported in Visual Basic 2008, using the event model you can duplicate and expand upon much of the control array functionality.

Conceptual Differences

In Visual Basic 6.0, control arrays could be used to manage controls on a form; they provided capabilities for sharing event handlers, iterating through groups of controls, and adding controls at run time.

In Visual Basic 2008, control arrays are no longer supported. Changes to the event model make control arrays unnecessary, and the .NET Framework provides the same capabilities for working with controls.

Sharing Event Handlers

In Visual Basic 6.0, control arrays could be used to specify a group of controls that shared a set of events. The controls had to be of the same type, and they had to have the same name.

Visual Basic 2008 allows any event handler to handle events from multiple controls, even controls with different names and of different types.

For example, you might add two Button controls (Button1 and Button2) and a CheckBox control (CheckBox1) to a form, and then create an event handler to handle the Click event for all three controls.

Private Sub MixedControls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, CheckBox1.Click

Iterating Through Controls

Another feature of Visual Basic 6.0 control arrays was the ability to iterate through a group of controls using the Index property. For example, to clear the text of all TextBox controls in a control array, you could loop through the control array using the Index property as a loop variable.

Visual Basic 2008 controls do not have an Index property, but you can still iterate through the controls on a form or container using the Control..::.ControlCollection of the Control class.

In Visual Basic 6.0, controls in a single control array could be sited on different containers. For example, TextBox controls contained on two different Frame controls could be part of the same control array.

In Visual Basic 2008, the Controls collection only returns controls sited on a single container. You must iterate through the controls of each container control separately; this can be done using a recursive function.

Adding Controls at Run Time

In Visual Basic 6.0, controls could be added to a control array at run time using the Load statement. The controls had to be of the same type as the control array, and the control array had to be created at design time with at lest one element. After adding the control, the Visible property had to be set to True.

In Visual Basic 2008, controls are added at run time by using the New keyword in a Dim statement, then using the Add method for the container where you want to add the control.

Adding Event Handlers at Run Time

In Visual Basic 6.0, when you added a control to a control array at run time, the new controls events were automatically handled by the events for the control array.

In Visual Basic 2008, you need to define event handlers for controls added at run time. This is accomplished using the AddHandler statement.

Code Changes for Control Arrays

The following code illustrates the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Sharing Event Handlers

The following example demonstrates sharing the Change event handler (TextChanged in Visual Basic 2008) for a group of three TextBox controls. In Visual Basic 2008, 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.

' Visual Basic 6.0

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

' Visual BasicPrivateSub TextBoxes_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged, _
TextBox2.TextChanged, TextBox3.TextChanged
    SelectCaseDirectCast(sender, TextBox).Name
        Case TextBox1.Name
            MsgBox("The text in the first TextBox has changed")
        Case TextBox2.Name
            MsgBox("The text in the second TextBox has changed")
        Case TextBox3.Name
            MsgBox("The text in the third TextBox has changed")
    EndSelectEndSub

Note

The event behavior is slightly different in Visual Basic 2008. The TextChanged event is raised when each control is initialized (for example in the Form_Load event) as well as when the text is changed at run time. In Visual Basic 6.0, the Change event was only raised when the text was changed.

Iterating Through Controls

The following example demonstrates a function for iterating through a group of text-box controls and clearing their text. In the Visual Basic 6.0 example, the Index property of a control array is used as a loop variable.

In Visual Basic 2008, a Control object is passed as an argument; it has a Control..::.ControlCollection collection that includes all controls sited on that control. The Typeof operator is used to determine if each control is of type TextBox.

Note

A Form object is of type Control; you can also pass a Form as an argument.

Because nested controls are not included in the Control..::.ControlCollection collection, the HasChildren method is used to determine if each control contains other controls, if so the ClearText function is called recursively.

' Visual Basic 6.0

Private Sub ClearText()

For i = 0 To Text1().UBound

Text1(i).Text = ""

Next

End Sub

' Visual BasicPrivateSub ClearText(ByVal container As Control)
    Dim ctrl As Control
    ForEach ctrl In container.Controls
        IfTypeOf (ctrl) Is TextBox Then
            ctrl.Text = ""EndIfIf ctrl.HasChildren Then
             ClearText(ctrl)
        EndIfNextEndSub

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, the control is added to a control array. In Visual Basic 2008 the control is added to the Control..::.ControlCollection collection. In Visual Basic 6.0, events for the new TextBox were automatically handled by the control array. In Visual Basic 2008, you need to hook up event handling through the AddHandler statement.

Both examples assume that a text-box control is added to the form at design time, and in the Visual Basic 6.0 example that a single-element control array was created. The Visual Basic 2008 example also assumes that an event handler named TextChangedHandler exists for the first TextBox control.

' 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

' Visual Basic' Declare a new TextBox.Dim TextBox2 AsNew TextBox
' Set the location below the first TextBox
TextBox2.Left = TextBox1.Left
TextBox2.Top = TextBox1.Top + 30
' Add the TextBox to the form's Controls collection.Me.Controls.Add(TextBox2)
AddHandler TextBox2.TextChanged, AddressOf TextChangedHandler

Upgrade Notes

When an application created with Visual Basic 6.0 is upgraded to Visual Basic 2008, any control arrays are upgraded to special control-specific control array classes. These classes are contained in the Microsoft.VisualBasic.Compatibility.VB6 namespace and are used by the upgrade tools to emulate Visual Basic 6.0 control array behavior.

Although it is possible to use these control array classes in new Visual Basic 2008 development, we recommend that you use the .NET Framework event model and functions instead.

See Also

Concepts

Events and Event Handling for Visual Basic 6.0 Users

Other Resources

Windows Forms Controls for Visual Basic 6.0 Users