-1

I want to enable all controls on a Panel. I can loop through the Panel like below. This doesn't touch all the components though, just the top level ones. How do I do this? (This is not the answer)

private void LoopThroughAllControls(System.Windows.Forms.Panel panel)
{
    for (int i = 0; i < panel.Controls.Count; i++)
    {
        panel.Controls[i].Enabled = true;
    }
}

ToolStrip code that adds ToolStripButton items:

this.toolStripContractor.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsbContractor_AddFile,
this.tsbContractor_View,
this.tsbContractor_Delete});
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • 5
    You need to make that recursive. – SLaks Jan 24 '18 at 22:27
  • I was trying that but not getting very far. What Class with the input parameter be? – Al Lelopath Jan 24 '18 at 22:29
  • 2
    Use the base `Control` class. – SLaks Jan 24 '18 at 22:31
  • 2
    I'm pretty sure you only need to do `Panel.Enabled = true` and it will automatically enable/disable all child controls anyway. You shouldn't need to manually enable/disable all children. – Dai Jan 24 '18 at 22:32
  • Look the like solution in https://stackoverflow.com/questions/15186828/loop-through-all-controls-on-a-form-even-those-in-groupboxes/35557077#35557077 – Georg Jan 24 '18 at 22:36

1 Answers1

5

All you have to do is set the Panel's Enabled property to true or false.

From MSDN's page on the Enabled property:

When a container control has its enabled property set to false, all its contained controls are disabled, as well. For example, if the user clicks on any of the controls contained in a disabled GroupBox control, no events are raised.

If, however, you choose to go with a recursive method, here is how to write it:

void SetEnabledAllChildrenOf(Control control, bool enabled)
{
    control.Enabled = enabled;
    foreach(Control c in control.Controls)
    {
        SetEnabledAllChildrenOf(c, enabled);
    }
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Thanks for your response. I'm having some difficulty and it may be the same difficulty I was having with setting the panel Enabled property to true ... it appears that buttons (i.e. items) on a ToolsStrip are not child controls. – Al Lelopath Jan 25 '18 at 16:45
  • @AlLelopath I can't help with code I can't see. If you can edit your question in include an [mcve] including the designer generated code then someone might be able to help you. – Zohar Peled Jan 26 '18 at 08:28
  • The code is your recursive method above. When it gets to the ToolStrip on the Panel, it thinks that Toolstrip does not have any children, so the recursion stops, so to speak. I've added to my original question the code that adds items to the ToolStrip. – Al Lelopath Jan 26 '18 at 14:14
  • ToolStrip items belongs to the `Items` property, since they are not inheriting `Control` the can't be referenced as `Control`. However, once you set the `ToolStrip`'s `Enabled` property to `false`, all it's items should be disabled with it. – Zohar Peled Jan 26 '18 at 14:26