Iterate all controls in BindingNavigator

525 Views Asked by At

I have some windows forms that each has some controls including buttons,ComboBox,... and also in each form i have a Bindingnavigator control that i added some new Toolstrip buttons to it, how can i write a general method that get 3 parameters and iterate all controls on a form(including that toolstrip buttons) and enable/disable Enabled status of an special control? my method signature is like this:

Public SetStatusOf(Form frm,string controlName,bool status)
1

There are 1 best solutions below

1
On

From the question, and from what I understood, you need this:

    foreach (Control c in frm.Controls)
    {
        if (c.Name.Equals(controlName))
            c.Enabled = status;
    }

but you can also directly use

frm.Controls[controlName].Enabled = status;