» Mimicking VB6 Control Arrays in VB.NET by Geoff Alexander aka PUN1SH3R
Page 1 of 1
(Login to remove green text ads)
Mimicking a Control Array
MS decided to take out control arrays in VB.NET. Why we are not sure. This is a missed feature for a lot of programmers. This article will show you a cut and dry way to mimick a control array.
The first thing we need to do is create a new project.
Open Visual Studio and create a new VB.NET Windows Application.
Next add about 5 textboxes. I have kept the default names because they will work with this example. The default names auto increment a numeric value at the end of the control name. This is key as it will act as the unique identifier for each of the text boxes.
Next what I have done is I have added another textbox (name does not matter) and a command button. These extra control are for demo purpose only. What will happen is I will type a value from 1 to 5 into the new textbox and press the command button. The form will then take the value I have typed and find the corresponding textbox and in turn place the value into the correct textbox.
Next we can start a new sub for the command button. Double click the command button in the form designer. This will automatically create a on_click event for the control.
Once the new event is created and the code window is shown we will start by declaring some variables. We will need a total of 2 variables. The 2 variables are only used as counters.
Code:
Dim int_CNTR as Integer
Dim int_CTL_COUNT as Integer
Once the variables are declared we can now proceed to mimick a control array. First thing we must do is create a couple of loops.
The first loop sets and increments our first variable. The count that is used in this loop will be determined by how many controls you want to loop through. Since we have 5 textboxes then we will count to 5.
Code:
For int_CNTR = 1 To 5
Next
The next loop will loop through the forms control set. This can be seen if you expand the "Windows Form Designer generated code" region. Depending on where you have placed your textboxes or any other controls you want to loop through will determine where the controls exist. I mean this by if we had placed our controls inside a groupbox for example, then we would have to search the groupbox controls set. But since the controls are only on the form, we can just specify the default controls set.
Code:
For int_CNTR = 1 To 5
For int_CTL_COUNT = 0 to Controls.Count
Next
Next
Next we will have to determine what kind of control is currently being looked at in the loop. We will have to specify what kind of control we are looking for and compare it with the current control in the control set. We can do this by using the TypeOf operator.
Code:
For int_CNTR = 1 To 5
For int_CTL_COUNT = 0 to Controls.Count
If TypeOf Controls(int_CTL_COUNT) Is TextBox Then
End IF
Next
Next
The next step is to determine if the name matches. Remember how we left the default names for the text boxes when we created them? This is where they come into play. The value we typed in the txtWhoText textbox will be the deciding factor on which textbox gets the value. We do this by using the default name and adding the value to the end of it.
Code:
For int_CNTR = 1 To 5
For int_CTL_COUNT = 0 to Controls.Count
If TypeOf Controls(int_CTL_COUNT) Is TextBox Then
If Contols.Item(int_CTL_COUNT).Name = "TextBox" & txtWhoText.Text Then
End If
End IF
Next
Next
Now we have found the textbox we are looking for. Now it is time to add the value we typed in to the value of the textbox. What we will have to do is declare a new variable as a textbox and reference the current textbox is the control set and then place the valuie in it.
Code:
For int_CNTR = 1 To 5
For int_CTL_COUNT = 0 to Controls.Count
If TypeOf Controls(int_CTL_COUNT) Is TextBox Then
If Controls.Item(int_CTL_COUNT).Name = "TextBox" & txtWhoText.Text Then
Dim ctl_TextBox As TextBox = Controls(int_CTL_COUNT)
ctl_TextBox.Text = txtWhoText.Text
Exit For
End If
End IF
Next
Next
Now we can compile our code and give it a try. Now if you put any number in lower than 1 or higher than 5 you will get an error but you can use error trapping methods in your applications.
There you have it. You have now successfully mimicked a control array. Word is that MS will be bringing back Control Arrays in the 2005 verison of Visual Studio. have to wait and find out.