Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forms : Enabled/Disable all controls in a container (panel)

I am coding a C# Forms application and would like to know how to enable/disable all controls container within a panel.

Here is my code:

private void EnabledPanelContents(Panel panel, bool enabled)
{
    foreach (var item in panel.Controls)
    {
        item.enabled = enabled;
    }
}

There is no enabled property in the panel.Controls collection.

How can I enable/disable all controls container within a panel.

Thanks in advance.

like image 678
user3736648 Avatar asked Apr 13 '15 08:04

user3736648


1 Answers

You are getting controls as var and iterating on them and var doesn't contain any property Enabled. You need to loop through controls and get every control as Control. Try this

private void EnabledPanelContents(Panel panel, bool enabled)
{
    foreach (Control ctrl in panel.Controls)
    {
        ctrl.Enabled = enabled;
    }            
} 

Enabled can be true or false.

like image 68
Mairaj Ahmad Avatar answered Oct 09 '22 18:10

Mairaj Ahmad