Reputation: 783
I know it seems like an odd request but I have this layout. With it I'm very much enjoying and taking advantages of the FlowLayoutPanel. I disable it to disable all child controls. But there are cases I have a link in there I want enabled all the time. Is this possible?
flowLayoutPanel1.Enabled = false; // disable panel and all child controls
linkLabel1.Enabled = true; // re-enable somehow the link that was disabled inherently
I'm only asking because there maybe a way to do this, but if it's officially a bad design case then I will rethink my solution.
Previously to get around this I overlayed the control I want to enable/disable independently of the parent control. I would line it up and position the control so it appeared to be with the group. I hated this. It makes design difficult as drag and drop just puts it back into the container and also totally defeats the purpose of using a FlowLayoutPanel.
Thanks in advance.
Upvotes: 1
Views: 492
Reputation: 783
Using the suggestion from Anthony. I've managed the following code that solves my dilemma. Using LINQ! (I always forget about using LINQ) I adjusted a bit with the extension methods and ability to just hammer in controls through a parameter array of what I do not want disabled. Very flexible to say the least.
private void button1_Click(object sender, EventArgs e)
{
// "reset"
flowLayoutPanel1.EnableAll();
}
private void button2_Click(object sender, EventArgs e)
{
// disable but the provide controls
flowLayoutPanel1.DisableBut(checkBox1);
}
public static class Extensions
{
public static void EnableAll(this FlowLayoutPanel container)
{
foreach (Control control in container.Controls.OfType<Control>())
control.Enabled = true;
}
public static void DisableBut(this FlowLayoutPanel container, params Control[] but)
{
foreach (Control control in (container.Controls.OfType<Control>().Where(x => (!but.Contains(x)))))
{
control.Enabled = false;
}
}
}
With suggested tweaks for performance, extensibility, etc.
public static class Extensions
{
public static void EnabledBut(this Control container, Boolean enabled, params Control[] but)
{
foreach (Control control in (container.Controls.OfType<Control>().Except(but)))
control.Enabled = enabled;
}
}
Upvotes: 1