views:

132

answers:

2

I need for example to disable all buttons in a form or validate all textbox's data... any Ideas.. thanks in advance!!!

+8  A: 

The simplest option may be to cascade:

public static void SetEnabled(Control control, bool enabled) {
    control.Enabled = enabled;
    foreach(Control child in control.Controls) {
        SetEnabled(child, enabled);
    }
}

or similar; you could of course pass a delegate to make it fairly generic:

public static void ApplyAll(Control control, Action<Control> action) {
    action(control);
    foreach(Control child in control.Controls) {
        ApplyAll(child, action);
    }
}

then things like:

ApplyAll(this, c => c.Validate());

ApplyAll(this, c => {c.Enabled = false; });
Marc Gravell
A: 

Also try:

public List<Control> getControls(string what, Control where)
    {
        List<Control> controles = new List<Control>();
        foreach (Control c in where.Controls)
        {
            if (c.GetType().Name == what)
            {
                controles.Add(c);
            }
            else if (c.Controls.Count > 0)
            {
                controles.AddRange(getControls(what, c));
            }
        }
        return controles;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var c = getControls("Button", this);

    }
Luiscencio