Skip to content

VB.NET – Control Arrays

October 8, 2005

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 and programming their events was quite simple. A single event handler used to fulfill 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.

  1. Placing the controls on the form at design time and writing their common event handler
  2. 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 first. Follow the steps below and do as it is requested. Explanation of this example will follow next:

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 (You can also copy/paste from here):

Public Sub CommonEvents(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

MsgBox (sender.tag)

End Sub

This is a simple routine. But you must have noticed that there is an ‘Index’ value which is missing. Well, that is the main tragedy here. 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’, ‘CheckBoxArray’ etc) provided by Microsoft with VB.NET to incorporate VB6 control arrays technique.

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 Microsoft with VB.NET to use control arrays

A ‘ButtonArray’ object is used to create a control array of command buttons. This object resides in the package Microsoft.VisualBasic.Compatibility.VB6.ButtonArray

Follow the following steps to implement control arrays through ‘ButtonArray’ object.

Declare the ‘ButtonArray’ object first as follows:

Dim WithEvents BArray As Microsoft.VisualBasic.Compatibility.VB6.ButtonArray

You will notice the keyword ‘WithEvents’ here in the declaration statement. This keyword is very important as it enables us to write the event handlers for the ‘ButtonArray’ object.

Now, place three command buttons at the VB Form. If your form is blank, the buttons will automatically be named as ‘Button1′, ‘Button2′ and ‘Button3′ else you should manually rename them to these names respectively.

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()
BArray.SetIndex(Button1, 0%)
BArray.SetIndex(Button2, 1%)
BArray.SetIndex(Button3, 2%)

This code actually initializes 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 it’s 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)

MsgBox(index)

End Sub

Now, this is quite similar to the VB6 control arrays’ event handlers. There is just a little 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 number. 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 which triggers that 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.

Comments

Tell us what do you think.

There are no comments on this entry.

Trackbacks

Websites mentioned my entry.

There are no trackbacks on this entry

Add a Comment

Fill in the form and submit.