how to list controls in an array?

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hello,

I am just starting out with C#. I have a set of controls
on a form that I set .Enable = true; or .Enable = false;.
Rather than listing all the controls all over the place, I
would like to list them in an array, ArrayList... so that
I can loop through the array

for (int i; i < myArr.Lenght, i++) myArr(i).Enable = true;

Can I do this (or something like this)?

ArrayList myArr(group1, group2, panel1, label1, txt1);

what is the correct syntax/correct collection object?

Thanks
Steve
 
I think I figured this out. At least the following
works. Any additional suggestions are welcome:

private void btnTest_Click(object sender, System.EventArgs
e)
{
Control[] arr = new Control[] {grpControl1, grpControl2};
foreach(Control ctl in arr)
ctl.Enabled = true;
}


Forgot about the "new" keyword.
 
Hi Steve,

Sure you can loop through the controls.

The simplest one would be

foreach(Control c in myArr)
{
c.Enabled = true;
}

Another solution which would be faster for arrays, but maybe not for arraylists

for(int i = 0; i < myArr.Count; i++) // Length for arrays, Count for Arraylist
{
Control c = (Control)myArr; // not necessary if you use Control[]
c.Enabled = true;
}
 
Back
Top