Click here to Skip to main content
15,916,432 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have roughly 40 labels on my form. I'd like to allow the user to change Forecolor of the labels, and then save that color as a default upon closing the form. (Clicking the form itself will remove color changes to both the form and the labels, restoring them to the colors that came with the install).

I have it working fine with the Backcolor of the form, but cannot get it to work with the labels. I suspect the difference is that I was able to bind the Backcolor to the form, whereas I don't know how to do that with all the labels as a group".

Thank you

What I have tried:

The ability to change colors works fine. My problem is saving it as a default so it's the same color next time it opens again.

This code changes the color of all labels on my form, but won't save the change.

private void btnFcolor_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)

                foreach (Control l in Controls)
                {
                    if (l.GetType() == typeof(System.Windows.Forms.Label))
                    {
                        l.ForeColor = cd.Color;
                    }
                }



This code changes, and saves the Backcolor of the form itself, and saves it on exit.

private void btnColor_Click(object sender, EventArgs e)
        {
            using (var dlg = new ColorDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    this.BackColor = Properties.Settings.Default.FormBackColor = dlg.Color;
                    Properties.Settings.Default.Save();
                }
Posted
Updated 31-Jan-20 9:49am
Comments
Richard MacCutchan 31-Jan-20 12:07pm    
You forgot to include the code to save the setting.
Smeezy 31-Jan-20 12:09pm    
I had it there, but it doesn't work. That's why I'm here.
Richard MacCutchan 31-Jan-20 12:13pm    
Well you need to explain what is meant by "it doesn't work.".
Smeezy 31-Jan-20 12:18pm    
The same line of code I used for the FormBackColor (adjusted for FormForeColor of course) just gives me errors when I try it with the forecolor. I thought it might be due to the "foreach" iterating through the labels. On a scale of 1-10 with coding, I'm at a solid 0.75, so beyond that, I cannot give you a better answer. It just gave me underline errors.
Richard MacCutchan 31-Jan-20 12:30pm    
"just gives me errors"
Then please tell us what the errors are!

Quote:
My problem is saving it as a default so it's the same color next time it opens again.


You need to override OnLoad[^] method:
C#
protected override void OnLoad(EventArgs e)
{
    //get color from settings
    color = ...
    this.BackColor = color;
    SetLabelColor(this.Controls, color);
    base.OnLoad(e);
}


So, create an empty form and name it ColorForm and put above code inside there. Then, your basic form has to inherits from ColorForm.

C#
public partial class YourBaseForm : ColorForm
{
 //
}
 
Share this answer
 
v2
Controls can be nested within other controls; they're not all direct children of the form.

You'll need a recursive method to process the entire control tree:
C#
private void btnFcolor_Click(object sender, EventArgs e)
{
    using (var dlg = new ColorDialog())
    {
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            SetLabelColor(Controls.Cast<Control>(), dlg.Color);
            Properties.Settings.Default.FormForeColor = dlg.Color;
            Properties.Settings.Default.Save();
        }
    }
}

private void SetLabelColor(IEnumerable<Control> controls, Color color)
{
    foreach (Label label in controls.OfType<Label>())
    {
        label.ForColor = color;
    }
    foreach (Control control in controls.Where(c => c.HasChildren))
    {
        SetLabelColor(control.Controls.Cast<Control>(), color);
    }
}
 
Share this answer
 
v3
Comments
Smeezy 31-Jan-20 13:40pm    
Thank you Richard. I tried your code, but get "squiggly line" errors. Specifically the words "Where" and "Controls" (with the capital C). I don't quite understand everything you said here. Is there somewhere I can read about this? Do I need to add a "using" or reference or something?
Richard Deeming 31-Jan-20 13:47pm    
Where and OfType are LINQ methods. You'll need using System.Linq; at the top of your file.

Language-Integrated Query (LINQ) (C#) | Microsoft Docs[^]
Smeezy 31-Jan-20 13:48pm    
I have that in there already.
Richard Deeming 31-Jan-20 13:50pm    
Ah, sorry; I forgot that the controls collection isn't strongly-typed.

You'll need a .Cast<Control>() before the .Where(...) method to make it work.
foreach (Control control in controls.Cast<Control>().Where(c => c.HasChildren))
Richard Deeming 31-Jan-20 13:52pm    
Or, if that's looking a bit scary, you could move the condition inside the loop:
foreach (Control control in controls)
{
    if (control.HasChildren)
    {
        SetLabelColor(control.Controls, color);
    }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900