簡體   English   中英

C#-一次修改所有窗體控件的最佳方法?

[英]C# - Best way to modify all form controls at once?

動態修改WinForm應用程序的每個控件(包括按鈕,工具欄,面板等)的前景色和背景色的最佳方法是什么? 是否有一種簡單的方法可以自動循環瀏覽每個控件,還是必須手動更改每個控件? 謝謝。

您可以循環瀏覽控件,我相信所有控件都有一個Controls屬性,該屬性是包含的控件的列表。

假設功能:

public void ChangeControlsColours(Controls in_c)
{

    foreach (Control c in in_c)
    {
        c.BackColor = Colors.Black;
        c.ForeColor = Colors.White;
        if (c.Controls.length >0 ) //I'm not 100% this line is correct, but I think you get the idea, yes?
            ChangeControlsColours(c.Controls)
    }

}
foreach (Control c in MyForm.Controls) {
    c.BackColor = Colors.Black;
    c.ForeColor = Colors.White;
}

這實際上取決於您要執行的操作。 最優雅的方法可能是在設計時定義的鏈接應用程序設置,然后就可以在運行時進行更改。

    private void UpdateInternalControls(Control parent)
    {
        UpdateControl(parent, delegate(Control control)
                                {
                                    control.BackColor = Color.Turquoise;
                                    control.ForeColor = Color.Yellow;
                                });
    }

    private static void UpdateControl(Control c, Action<Control> action)
    {
        action(c);
        foreach (Control child in c.Controls)
        {
            UpdateControl(child, action);
        }
    }

暫無
暫無

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

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