8

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.

3
  • 2
    why cant you use item.enabled=false Commented Apr 13, 2015 at 8:58
  • 4
    You could set the enable property of the panel to false Commented Apr 13, 2015 at 9:09
  • @horHAY Disabling the panel will also disable its scrollbars, which is not necessarily the intent of the OP.. Commented Jan 20, 2024 at 15:53

6 Answers 6

17

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.

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

3 Comments

Voting Up, and I think it is better for Typing ctrl.Enabled = enabled; instead of ctrl.Enabled = true;
@ahmedabdelqader you are right but i just gave example so just used true instead of enabled thanks for your suggestion. I will update the answer.
Why am i getting this? "system.web.ui.control' does not contain a definition for 'enabled'"
5

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

A: If you want to disable or enable all controls within a panel, you can directly call,

Panel panel; -> panel.Enabled = true;//For enabling all controls inside the panel. -> panel.Enabled = false;//For disabling all controls inside the panel. 

If you want only specific controls inside the panel to be enabled or disabled then you can iterate through the collection of controls and set it's enable state to true or false based on your requirement.

1 Comment

This seems is a much better answer than the accepted answer. It both solves the problem, but also handles such situations as recursion. Though not targeted at XAML, it does allow (in XAML) for one to bind the enabled state to a variable, simplifying code requirements.
1

If you declare item as var (in the foreach loop), it won't have the properties of a windows control. You should declare it as a control.

Try this code snippet and it should work:

foreach (Control item in panel.Controls) { item.Enabled = true; // = true: enable all, = false: disable all } 

Comments

1

Try the following code,

 private void DisableAll_Click(object sender, EventArgs e) { EnabledPanelContents(this.panel1, false); } private void EnabledPanelContents(Panel panel, bool enabled) { foreach (Control item in panel.Controls) { item.Enabled= enabled; } } 

Comments

-1

@anshu

if your controls are HTML controls then use

foreach (Control ctrl in myControl1.Controls) if (ctrl is HtmlControl) ((HtmlControl)ctrl).Disabled = true; 

Comments

-1
private void Form1_Load(object sender, EventArgs e) { foreach (Control item in panel.Controls) if (ctrl is Button) ((Button)item).Enabled = false; } 

1 Comment

I have fixed the formatting in your answer. In future you should correctly format your posts to make them readable. From review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.