Need to use a color dialog result in a separate Windows Form

1.3k views Asked by At

Please go easy on me I am very new to coding and stuck on something that I'm sure is very simple.

Currently I have an Options form in which the user can select from the standard colordialog.showdialog() and the result is displayed as a sample on the form as a label

If(backColorDialog.Showdialog() == DialogResult.OK); 
   backColorLabel.BackColor = backColorDialog.Color; // set to label to show option selected

I need to take that selected color and apply it to the background color of a "game board" in another Windows form. I have already added a reference from the "game board" form to the options form.

The "Game Board" is laid over a TableLayoutPanel so I need to be able to change the BackColor of the panel

TableLayoutPanel1.BackColor = (colordialog result from options form)

Like I said I am new to this and appreciate any help you can provide

1

There are 1 answers

6
BartoszKP On

Create a property on your configuration form:

Color SelectedColor { get; private set; }

And assign users choice to it:

if (backColorDialog.Showdialog() == DialogResult.OK)
{
    backColorLabel.BackColor = backColorDialog.Color;
    SelectedColor = backColorDialog.Color;
}

(note there was a mistake in your code - the ; after if line)

After this, in your main form you can read it from your configuration form:

TableLayoutPanel1.BackColor = optionsForm.SelectedColor;

As noted by justin.chmura in the comments below, you can also consider a different approach using events. Your options form would raise an event when user selects a different color, and main form's event handler would apply this color instantly on the game panel. The difference is that it would update the color immediately. When it's not needed the above solution with a property is enough.