One of the most embarrassing things in VB.NET is that it lacks ‘Control Arrays’. Control Arrays was a very important feature in VB6. Through this feature, we used to place more than one control of the same type with the same name on the VB Form. These controls were identified by their indexes so as a result, programming their events was quite simple. A single event handler was used to fulfil the task for the rest.
VB.NET doesn’t provide control arrays. But it doesn’t mean that this language no more supports control arrays. There are some different techniques provided by Microsoft to use the control arrays. Following are the two ways through which you can use the control arrays in VB.NET.
- Placing the controls on the form at design time and writing their common event handler
- Using the standard VB6 control array objects (like ‘ButtonArray’, ‘LabelArray’, ‘CheckBoxArray’ etc) provided by Microsoft with VB.NET to incorporate VB6 control arrays technique
Let’s discuss both of these scenarios one by one.
Placing the controls on the form at design time and writing their common event handler
This is the approach that Microsoft recommends. As per this process, you first have to add the controls that you want to use in the control arrays form and then manually write a common event handler for all of them. Let us implement it below.
1. Start a new project in VB.NET.
2. Add three command buttons to the VB Form. They will automatically be renamed to Button1, Button2, and Button3.
3. Setup their tag values to 1, 2 and 3.
3. Right-Click at the form’s blank area and select ‘View Code’
4. Place the cursor at the bottom of the code area and write the following sub-routine:
Public Sub CommonEvents(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
This is a normal sub-routine. But you must have noticed that there is an ‘Index’ value which is missing. Well, you can no longer use the index value of a control. Instead, you have to specify an index value in the Tag value of control as we assigned the Tag values to all the three buttons as 1, 2 and 3.(Using the Tag value as an index is not mandatory here and you can follow any other practice as well. But this is the simplest and most commonly used practice)
The main trick is done by the ‘Handles’ keyword. This is a new keyword introduced in VB.NET. The ‘Handles’ keyword here makes this sub-routine a common event handler for the click events of all the three buttons. A ‘sender’ object which is one of the two parameters of this routine is the object which has triggered the event. If you press ‘Button1’, ‘Sender’ object will be set to ‘Button1’ and then the message box will be shown displaying it’s tag. Same rule will apply for ‘Button2’ and ‘Button3’.
Using the standard VB6 control array objects (like 'ButtonArray', 'LabelArray' etc.)
Using standard VB6 control array objects is the second approach of implementing control arrays in VB.NET. VB.NET provides all the objects for managing every VB6 control arrays like ‘ButtonArray’, ‘LabelArray’, ‘CheckBoxArray’ etc. Each object is used to hold arrays of it’s type. For e.g. A “ButtonArray’ is used to hold command butons in an array, a ‘LabelArray’ is used to hold labels in an array and a ‘CheckBoxArray’ is used to hold CheckBoxes in an array. All these objects are provided in the package Microsoft.VisualBasic.Compatibility.VB6. Let’s discuss this by an example of a ‘ButtonArray’.
Using the 'ButtonArray' object provided by VB.NET to use control arrays
A ‘ButtonArray’ object is used to create a control array of command buttons. It resides in the package Microsoft.VisualBasic.Compatibility.VB6.ButtonArray
Follow the steps below to implement control arrays through ‘ButtonArray’ object.
Declare the ‘ButtonArray’ object first:
Dim WithEvents BArray As Microsoft.VisualBasic.Compatibility.VB6.ButtonArray
The keyword ‘WithEvents’ enables us to write event handlers for the ‘ButtonArray’ object.
Place three command buttons on the VB Form. If your form is blank, the buttons will automatically be named as ‘Button1’, ‘Button2’ etc. otherwise you should manually rename them.
Now, add the following code to the code window. It is preferred that you put this code in the Form’s Load Event:
BArray = New Microsoft.VisualBasic.Compatibility.VB6.ButtonArray()
This code initialises the ‘ButtonArray’ object first and then adds the three buttons to the ‘ButtonArray’ collection. It uses ‘SetIndex’ function of the ‘ButtonArray’ object which takes two parameters: First one is the object or component that you want to add and the second one is the index value that you want it to have for its reference later.
Now, you have to write it’s event handler as follows:
Private Sub BArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim index as Integer
index = BArray.GetIndex(sender)
This is quite similar to the VB6 control arrays’ event handlers with a small difference. VB6 used to have index as the parameter of the event whereas here, you have to call ‘ButtonArray’ object’s ‘GetIndex’ function to retrieve the index. The ‘GetIndex’ function takes a command button as parameter which is the ‘sender’ object in our case. Remember we mentioned earlier that a ‘sender’ is an object in the event handler that triggers the event in a control array. Finally, the event handler shows the message box displaying the index number.
The ‘LabelArray’, ‘CheckBoxArray’ and all the other objects provided to manage control arrays in VB.NET work similar to this ‘ButtonArray’ object.