Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - using ColorDialog across forms

I have a windows form application. On the main form a user will enter a number of item, etc and then click a button which will open a new form (either a small form or a large form depending on a checkbox). Now on my main application I have a file menu - under which is settings - change background colour. This opens the colordialog. If a user does not pick anything the background colours will stay default. However if they change it on the main entry form i change the background of a few textboxes - code below.

private void warning1ToolStripMenuItem_Click(object sender, EventArgs e)
{
    colorDialog1.ShowDialog();
    Warn1Color = colorDialog1.Color.ToString();
    if (Warn1Color != null)
    {
        tbWarn1Hrs.BackColor = colorDialog1.Color;
        tbWarn1Mins.BackColor = colorDialog1.Color;
        tbWarn1Secs.BackColor = colorDialog1.Color;
        tbWarn1Msg.BackColor = colorDialog1.Color;
    }
}

Now my problem is how to I get this to then change the background in the other form that opens. I was hoping I could pass the string across in the new form constructor as i do with a number of other values.

i.e - here is my code in the new form....(note - string Warn1Color was passed across in constructor and then made = to the string _Warn1Color. If it is null then background will be default yellow but it cant convert type string to system.drawing.color. Does anyone see an easy solution to this or what I could do to get this working easily.

if (_Warn1Color == null)
{
    this.BackColor = System.Drawing.Color.Yellow;
}
else
    this.BackColor = _Warn1Color;
like image 389
Ctrl_Alt_Defeat Avatar asked Mar 13 '26 19:03

Ctrl_Alt_Defeat


1 Answers

Pass the Color on via the Constructor not a string. If this is not possibly for whatever reason, you could create a ColorConfigClass that holds the required Color and you can set it when used.

like image 81
Gambrinus Avatar answered Mar 16 '26 08:03

Gambrinus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!