Creating Control Arrays

Published Wed, Aug 2 2006 19:27 | Paul June

Control arrays are a very useful functionality in your application. They tend to simplify things that you are doing to a certain number of controls. For example, you want to disable a numerous number of textboxes, you wouldn’t have to code each textbox for disabling, instead you would just use a loop for disabling these certain controls.

 

In the Visual Basic 6.0 era, creating control arrays are just a snap. Creating controls with the same name would automatically array your controls. But in the .Net era you cannot just do this by the fly (on your Design mode). So, how could you do this? I will present 3 ways of achieving this, being the last way as the most efficient way.

 

The first approach is the crudest of them all. In this solution you intend to really create an array of controls. For the reason of demonstrating, let’s try creating an array of 50 textbox controls.

 

TextBox[] textboxes = new  TextBox[50];

for (int i = 0; i < 50; i++) {

textboxes[ i].Width = 100;

}

 

The problem as you see here is that you will manually place the controls in your form. Which would be quite problematic especially if you want to place controls which are not so relevant with each other.

 

The second approach is to traverse the controls collection of the form. This is a very effective solution. But the problem is you’ll have to always check the name and type of the control  if it matches the controls you want to traverse as seen in this code:

 

foreach ( Control c in this.Controls) {

if (c.GetType().ToString() == “System.Windows.Forms.TextBox” && c.Name == “myName”) {

….

}

}

 

The third and final solution is my preferred approach to this problem. First is you’ll have to name your controls in series. It means that your control names must have numeric values assigned to it. Think of it as an index for your control. Ex:

 

TextBox1, TextBox2, TextBox3, …

 

After that,  you can now loop using the controls collection. But instead of looping with all of the controls lets directly get the name:

 

for (int i = 0; i < 50; i++) {

TextBox txt = (TextBox) this.Controls[“TextBox” + i.ToString()];

Txt.Enabled = false;


}

 

Well, as you can see the third solution is pretty simple and straight forward. No name verifications are required and at the same time you can also use your design mode to place your controls on the form. The only drawback I can see with this approach is you could not name your control to its exact descriptive names. But I think it’s quite a little sacrifice imaging the lines of code that you’ll save with this approach.

Comments

# Alun Jones said on August 3, 2006 11:23 AM:

Why not use the Tag property?
Name the Control whatever you like, then give it a Tag in the Windows Forms designer, containing text that lists which control array(s) the Control belongs to.
Then you can build your array like this:
foreach ( Control c in this.Controls) {

if (c.Tag && ((String)c.Tag).Contains("Array1")) {

….

}

}

This way, too, a control can belong to multiple arrays, you get to call it by its descriptive name, and the world is happy.

Make sure, if you use the code I described, that you don't use tag elements that could be confused - Contains("Array1") will match "Array10", "Array11", etc.  You could easily get around this by delimiting the string, for instance as Contains(":Array1:"), with a tag that looks like ":Array1:Array2:Array3:".

# Creating Control Arrays » Wagalulu - Microsoft said on August 3, 2006 3:12 PM:

PingBack from http://microsoft.wagalulu.com/2006/08/02/creating-control-arrays/

# Ignacio Freiberg said on August 4, 2006 1:03 AM:

I recently had this problem, and I just used an ArrayList of controls.

The critical problem here is not how to make an array of controls; it's how to HANDLE EVENTS of those controls!

How do you work with the events of the arrayed controls? :S

# Paul June said on August 4, 2006 9:28 AM:

Well, handling events in an array control is still the same like handling events on single controls. You can either give you array of controls only 1 delegate function as an event and just check the sender parameter on who on the array triggered the event or you can assign different events for each controls in the array...

# Max R. said on May 9, 2007 1:16 PM:

Hello! Very interesting. Thank you.

# bathroom vanities said on July 31, 2007 10:48 AM:

bathroomdesign.greatnow.com/bathroom-vanities bathroom vanities

# bathroom vanities said on July 31, 2007 10:48 AM:

bathroomdesign.greatnow.com/bathroom-vanities bathroom vanities

# bathroom faucets said on August 2, 2007 3:18 PM:

bathroomdesign.greatnow.com/bathroom-faucets bathroom faucets

# bathroom vanity cabinet said on August 4, 2007 12:43 PM:

bathroomdesign.greatnow.com/bathroom-vanity-cabinet bathroom vanity cabinet

# bathroom vanity said on August 4, 2007 8:03 PM:

bathroomdesign.greatnow.com/bathroom-vanity bathroom vanity

# airbrcgueo said on August 26, 2007 1:29 PM:

Hello! Good Site! Thanks you! yevpragdlgma

Leave a Comment

Name:  
Website: