Control Arrays for Visual Basic 6.0 Users
Although control arrays are no longer supported in Visual Basic 2005, 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 2005, 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 2005 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 2005 controls do not have an Index property, but you can still iterate through the controls on a form or container using the 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 2005, 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 2005, 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 2005, 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 2005.
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.
' 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 Basic 2005 Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TextBox1.TextChanged, _ TextBox2.TextChanged, TextBox3.TextChanged Select Case DirectCast(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") End Select End Sub
Note |
|---|
| The event behavior is slightly different in Visual Basic 2005. 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 2005, a Control object is passed as an argument; it has a 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 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
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 2005 the control is added to the ControlCollection collection. In Visual Basic 6.0, events for the new TextBox were automatically handled by the control array. In Visual Basic 2005, 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 2005 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 2005 ' Declare a new TextBox. Dim TextBox2 As New 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 2005, 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 2005 development, we recommend that you use the .NET Framework event model and functions instead.
See Also
How about Text2(i).Something = Text1(i).something?
And if we have no idea about index i, and should ReDim Text2(size), Text1(size) during runtime?
In vb6 it was 1 line code.
We live so many years with Net, but even VS.Net 2010 does not have such function!
- 7/6/2011
- OkyToky
I did quite a bit of research this week on control arrays and why they weren't included. The most plausible reason I came across was that they ran out of time, or the coding was too complicated. I guess there hasn't been enough noise from consumers for them to attempt to put them back. I expect it would cause code compatibility issues anyway. (I'd like the vent windows back in car doors, too, but such is life.)
The examples on this page seem quite clear and I'm going to give it another go. Set up a module or two and it should be quite easy, actually. It has been silly of me to refuse to look into this just because they took out one of my favorite things. MS VB.Net is still easier than any other platform I've looked at, I expect that is because it still has the most familiarity for me.
- 12/27/2010
- LindaJHK
- 10/1/2010
- NobbyClark
Furthermore, I belive multi-dimensional control arrays should have been posible.
- 4/11/2009
- Bit Master
Note