Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I am new in the big programming community, and got a small issue with my app.
I'd like the users to be able to click on a button and set the colors of the ENTIRE APP, like a theme. Here is how it looks right now, and some snippets of code I tried:

What the form looks like[^]

As you can see, the button on the left using color1 (in Form1.cs), and even the button in the childform (Form_Settings.cs) don't change color at all.

Form_Settings.cs
C#
private void trackBarR_Scroll(object sender, EventArgs e)
        {
            buttonRGB1.BackColor = Color.FromArgb(trackBarR1.Value, trackBarG1.Value, trackBarB1.Value);
        }
// my click event not working
private void buttonRGB1_Click(object sender, EventArgs e)
        {
            ColorThemePicker.RGBColors.color1 = buttonRGB1.BackColor; // change variable from struct to the color set but doesn't work
            ActiveForm.Update(); // I tried to update the form but this doesn't seem to work
        }


ColorThemePicker.cs
C#
public struct RGBColors
            {
            // RGBColors.color1
            public static Color color1 = Color.FromArgb(173, 112, 133); // (Color 1) Very dark pink
            }


Am I missing something, or doing something wrong? All reference of the color I want to change are set as color1, example here of the button in the childform (Form_Settings.cs):

Form_Settings.Designer.cs
C#
this.color1.BackColor = RGBColors.color1; // here you can see it's set as the color1 from the struct and not the usual system colors
this.color1.FlatAppearance.BorderColor = System.Drawing.Color.Silver;
this.color1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.color1.Location = new System.Drawing.Point(407, 297);
this.color1.Name = "btn_color1";
this.color1.Size = new System.Drawing.Size(75, 67);
this.color1.TabIndex = 12;
this.color1.Text = "I\'m Color1";
this.color1.UseVisualStyleBackColor = false;


What I have tried:

A ton of googling, stackoverflow, youtube, and some research here as well, but nothing.
Tried to Reset(), Update(), etc the form to see any change: doesn't work.
Tried to set the new struct value to the trackbars values Color.FromArgb(trackBarR1.Value, trackBarG1.Value, trackBarB1.Value)
doesn't work either
Posted
Updated 14-Feb-21 6:36am
Comments
RickZeeland 14-Feb-21 6:30am    
Did you try to set ActiveForm.BackColor or this.BackColor ?
Opalie 14-Feb-21 7:58am    
I did! It doesn't work either, but thank you
Maciej Los 14-Feb-21 7:54am    
How many windows/forms contains your application? Only one or more?
Opalie 14-Feb-21 7:58am    
many: a main Form (Form1) + child forms to have inside of it (here, Form_Setting)
RickZeeland 14-Feb-21 8:46am    
Hope you are not using a "form in a form" that is a bad idea!

As to the comments to to question...

Maciej Los wrote:

How many windows/forms contains your application? Only one or more?
Opalie wrote:

many: a main Form (Form1) + child forms to have inside of it (here, Form_Setting)



If you would like to update BackColor for each loaded form, you need to loop through the collection of opened forms:
C#
foreach(Form f in Application.OpenForms)
  f.BackColor = aVariableWhichHoldsColor;


As to the aVariableWhichHoldsColor...
I'd suggest to define this variable in Program.cs:
C#
public static Color MyThemeColor = Color.FromArgb(173, 112, 133);

or create static class:
C#
public static class MyHelper
{
   //set default color
   public static Color MyThemeColor = Color.FromArgb(173, 112, 133);
}


Finally, in Form_Settings class you have to call method responsible for changing BackColor for each opened form:
C#
MyHelper.MyThemeColor = buttonRGB1.BackColor;
foreach(Form f in Application.OpenForms)
  f.BackColor = MyHelper.MyThemeColor;


Without knowing more details, it's hard to tell what you have to do to resolve your issue.

Good luck!
 
Share this answer
 
Try this:
WinForms Form Skin[^]
Or this:
Winforms SkinFramework[^]
Or this:
GUI Skinning System for Windows Forms .NET[^]
But Google will find you loads of other examples!
 
Share this answer
 

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