NateShoffner
NateShoffner

Reputation: 16829

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? Is there an easy way to cycle through each control automatically or do I have to manually change each one? Thanks.

Upvotes: 0

Views: 1105

Answers (4)

Lonli-Lokli
Lonli-Lokli

Reputation: 3766

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

Upvotes: 0

Mario
Mario

Reputation: 36487

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.

Upvotes: 0

You can cycle through controls, I believe that all controls have a Controls property that is a list of contained 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)
    }

}

Upvotes: 4

Will Marcouiller
Will Marcouiller

Reputation: 24132

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

Upvotes: 2

Related Questions