简体   繁体   English

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

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

What is the best way to dynamically modify the forecolor and background color of every control of a WinForm application consisting of buttons, toolstrips, panels, etc? 动态修改WinForm应用程序的每个控件(包括按钮,工具栏,面板等)的前景色和背景色的最佳方法是什么? Is there an easy way to cycle through each control automatically or do I have to manually change each one? 是否有一种简单的方法可以自动循环浏览每个控件,还是必须手动更改每个控件? Thanks. 谢谢。

You can cycle through controls, I believe that all controls have a Controls property that is a list of contained controls. 您可以循环浏览控件,我相信所有控件都有一个Controls属性,该属性是包含的控件的列表。

Hypothetical function: 假设功能:

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

It really depends on what you're trying to do. 这实际上取决于您要执行的操作。 The most elegant way might be a linked application setting you define on design time and you're then be able to change on run time. 最优雅的方法可能是在设计时定义的链接应用程序设置,然后就可以在运行时进行更改。

    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