2

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.

4
  • Have you actually tried implementing something similar to your pseudo code? It's along the lines that I would of thought might work. Commented Feb 4, 2010 at 20:46
  • did what you proposed not work? Commented Feb 4, 2010 at 20:46
  • I this windows forms or webforms (asp.net)? Commented Feb 4, 2010 at 20:46
  • Well first of all I can't create an array like that without specifying a size. Also if i used the example above there is not a add() method associated with ControlCollection. List<Control> works great though. Commented Feb 4, 2010 at 20:51

4 Answers 4

6

your example should be fine

List<Control>

will also work

Sign up to request clarification or add additional context in comments.

1 Comment

My example didn't work exactly as typed but using a list works great.
0

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.

Comments

0

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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.