簡體   English   中英

防止控件繼承其父母禁用狀態

[英]Prevent control from inheriting it's parents disabled state

我知道這似乎是一個奇怪的要求,但我有這種布局。 有了它,我非常享受並利用FlowLayoutPanel的優勢。 我禁用它以禁用所有子控件。 但是在某些情況下,我總是有一個要一直啟用的鏈接。 這可能嗎?

flowLayoutPanel1.Enabled = false; // disable panel and all child controls
linkLabel1.Enabled = true; // re-enable somehow the link that was disabled inherently

我之所以問是因為可能有辦法做到這一點,但是如果這在正式情況下是一個糟糕的設計案例,那么我將重新考慮我的解決方案。

以前為了解決這個問題,我疊加了要獨立於父控件啟用/禁用的控件。 我將其排列並放置控件,使其看起來像在組中。 我討厭這個 由於拖放操作將其放回容器中,因此使設計變得困難,並且完全無法使用FlowLayoutPanel的目的。

提前致謝。

使用安東尼的建議。 我管理以下代碼解決了我的難題。 使用LINQ! (我總是忘記使用LINQ)我對擴展方法進行了一些調整,並且能夠通過不希望禁用的參數數組來敲入控件。 至少可以說非常靈活。

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;
        }
    }

}

針對性能,可擴展性等進行了建議的調整。

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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM