An article about duplicating the VB 6 "control array" in VB.NET that I originally wrote a couple of years ago has generated more reader response than any other single topic.
What IS it about control arrays that generates such interest? I honestly don't know. But here's an index to the previous articles in the series:
VB.NET: What Happened to Control Arrays!!! (Part I)
VB.NET: What Happened to Control Arrays!!! (Part II)
VB.NET: What Happened to Control Arrays!!! (Part III)
VB.NET: What Happened to Control Arrays!!! (Part IV)
VB.NET: What Happened to Control Arrays!!! (Part V)
Patrick Krawec, one of the intrepid crew of programmers at Inovision Software Solutions has created a solution that must be "best of class" in several ways.
- Once the class is included in your project, it's really easy to use.
- It's an excellent example showing how to pass objects and collections to a class so that the involved details can be taken care of in the class. The logic itself is pretty straightforward and obvious. The basic idea is for the class to search the form for all controls that fit a common naming convention and stick them in an ArrayList. (An ArrayList is an interesting new class in .NET. Microsoft documents it this way: "An ArrayList is a sophisticated version of an array. The ArrayList class provides some features that are offered in most Collections classes but are not in the Array class.")
But possibly most important ...
- It actually does create something that does have many of the characteristics a good old VB 6 control array! But, like every solution proposed so far (including Microsoft's - the original Part I article), it does have some problems.
Here's how Patrick explains his solution:
I implemented a very simple solution which links Controls on a form to the code behind it as a control array, much like VB6.
I have come across the same problem as others. I have many screens with many control arrays that we created in VB 6.
I created a way to do this in the application code which requires only one function call. The call returns an array of the type of control desired. (Of course you need to include a helper class in the project, or compile it as a Class Library.)
The key is: You have to name your controls on the screen with a standard convention, so the function can find them.
Example: myTextBox1, myTextBox2, myTextBox3. This will fill the array with the three controls in positions 1, 2, and 3.
A sample of the application code is:
Dim textBoxes as TextBox() = getControlArray(me,"myTextBox")
Thats it! From that point the textBoxes array is very similar to a VB6 control array and the function works with any type of control.
Patrick's solution is a variation on the theme of previous solutions. Like Microsoft and several previous contributors, Patrick codes a class to do most of the work. In Part III, I complain that because the code doesn't inherit the class, things like changing the Height property of a control don't work. That's also the case here. William Benton, in Part V, shows how to do a VB.NET control array with an inherited class.
Selden McCabe, in Part IV of this series, pointed out that controls which aren't direct children of the form, such as Tabcontrol, won't be handled with this type of solution. Combining Selden's solution with Patrick's will be left as an exercise for the reader!
The great advantage of Patrick's solution is that it's easy to add and delete controls at design time. All you have to do is make sure they are named correctly. And gaps in the sequence are OK too. There are five Label and Textbox controls that are all named Mixed with a number suffix in a controls( ) collection in Patrick's example, I added a new Button control and named it Mixed29. The code ...
controls(29).Text = "About Visual Basic"
... worked perfectly!
To download Patrick's solution ... Click Here.
