Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a color dialog, one which is used to set the BackColor of the forms, however the global method I made to set it will not accept MainForm -- my other form than the settings form -- as the Form argument for the method.

Below is my code for the button:

C#
private void button1_Click(object sender, EventArgs e)
        {
            AppBackgroundColorDialog.ShowDialog();
            globalvariables.DefaultBackgroundColor = AppBackgroundColorDialog.Color;
            globalvariables.SetBackColor(this);

            //Below is what's in question
            globalvariables.SetBackColor(MainForm);

            /*I also tried what is beneath this comment, which works and makes more
            sense, but results in two MainForm(s). I must also note that it does not
            appear as well as simply changing the color of the already open form 
            would.*/
            Form newmainform = new MainForm();
            globalvariables.SetBackColor(newmainform);
            newmainform.Show();
        }


Below is the code for my global variables class and the method for setting form background color (as you can undoubtedly infer):

C#
public class globalvariables
    {
        public static Color DefaultBackgroundColor = Color.FromArgb(255, 255, 255);

        public static void SetBackColor(Form form)
        {
            form.BackColor = globalvariables.DefaultBackgroundColor;
        }
    }


I realize MainForm is a type, not a variable, but I'm not sure what to do about that or how to set the background color without using closing MainForm, then reopening it with the newly set background color. Do I have to call the method from within MainForm, or is there a way to do so from SettingsForm?

Thank you in advance!
Posted
Updated 20-Nov-14 13:29pm
v2
Comments
Wastedtalent 25-Nov-14 7:24am    
It might be that you need the using to prevent the dialog being disposed as soon as it's closed. This seems to work for me:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btChange_Click(object sender, EventArgs e)
{
using(ColorDialog dialog = new ColorDialog())
{
if(dialog.ShowDialog() == DialogResult.OK)
{
globalvariables.DefaultBackgroundColor = dialog.Color;
globalvariables.SetBackColor(this);

Form2 form2 = new Form2();
globalvariables.SetBackColor(form2);
form2.Show();
}
}
}
}

public class globalvariables
{
public static Color DefaultBackgroundColor = Color.FromArgb(255, 255, 255);

public static void SetBackColor(Form form)
{
form.BackColor = globalvariables.DefaultBackgroundColor;
}
}

1 solution

If you want to affect the current instance of the form, you just pass the current instance:
C#
private void button1_Click(object sender, EventArgs e)
        {
            AppBackgroundColorDialog.ShowDialog();
            globalvariables.DefaultBackgroundColor = AppBackgroundColorDialog.Color;
            globalvariables.SetBackColor(this);
        }
If what you are trying to do is alter the form that called the settings form from within the settings form, then it's more complex - and what you should do is have the settings form raise an event which the main form handles - and that changes it's own colour (or passes it's own instance to your SetBackColor method).
That sounds complicated, but it really isn't: Transferring information between two forms, Part 2: Child to Parent[^] explains how.
 
Share this answer
 
Comments
Logan Apple 25-Nov-14 17:34pm    
That's exactly what I was looking for; passing information from the child to the parent. Thank you very much for your assistance!
OriginalGriff 25-Nov-14 17:51pm    
You're welcome!

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