views:

233

answers:

4

I have a form which contains several standard controls (textbox's, buttons, etc). I want to group certain controls in collections so that I can enable and disable them at any given time without having to explicitly set each one. What is the syntax to do that? Here is some pseudo code to show what I want to do....

Control[] ControlCollection = new Control[];
ControlCollection.add(Button1);
ControlCollection.add(TextBox1);
...
...
foreach( Control x in ControlCollection)
{
    x.Enabled = false;
}

I know i could put the controls in say a group box and accomplish this but the controls are not positioned on the form in such a manner that it is convenient to do so.

+2  A: 

your example should be fine

List<Control>

will also work

Steven A. Lowe
My example didn't work exactly as typed but using a list works great.
Jordan S
A: 

Have you considered using a layout manager? (as far as positioning goes) Keeping a list of controls (without specifying the control's position) will not automatically position the controls, a layout manager could help.

aggietech
A: 

If your not already using the tag property of the control then you could put some form of controlid in the tag and then enumerate the controls collection looking for the particular id you want and enable/disable them.

David
A: 

Assuming you are using webforms and .net 3.5 you could have something like

var cntrls = new List<WebControl>()
        {
            {new TextBox(){ID = "Textbox1"}},
            {new Button(){ID="Button1", Text = "Click me!"}}
        };

cntrls.ForEach(x => x.Enabled = false);
simon_bellis