0

I have a project with 2 forms, the first form is empty. The second form has 5 buttons.

When I press a button, it opens a color dialog. I'm choosing a color, and button's background color is changing. For example, if button1's background color id is Green, form1's background color should be green. Not instant but when I press the Save button. I need to get button's background color into a variable. How can I do this?

private void btnKAMU_Click(object sender, EventArgs e)
{
    colorDialog1.ShowDialog();
    btnKAMU.BackColor = colorDialog1.Color;
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
DoganalpK
  • 13
  • 8
  • I think is better open the second form passing a reference to the first form than storing the new backgroud color in a variable. So you can simply use this reference to set new color when the user clicks save. – tezzo May 03 '18 at 15:01

1 Answers1

2

You can pass a reference of Form1 to Form2 setting Form2 Owner, with a custom Property or using Form2 constructor.

When, in Form1, you create an instance of Form2:

Using then Owner property:
(Form2 Owner is set when you instantiate a Form this way:
form2.Show(this);. The this reference is Form2 Owner - Form1 here).

Form2 form2 = new Form2();
form2.Show(this);
//form2.ShowDialog(this);

In Form2, set the Owner BackColor property:

private void btnSAVE_Click(object sender, EventArgs e)
{
    this.Owner.BackColor = btnKAMU.BackColor;
}


Using a custom property:
Form2 form2 = new Form2();
form2.Form1Reference = this;
form2.Show();
//form2.ShowDialog();

In Form2, using a property value:

public Form Form1Reference { get; set; }

private void btnSAVE_Click(object sender, EventArgs e)
{
    this.Form1Reference.BackColor = btnKAMU.BackColor;
}


Setting a reference of Form1 in Form2 constructor:
Form2 form2 = new Form2(this);
form2.Show();
//form2.ShowDialog();

With a property value as before:

private Form Form1Reference { get; set; }

public Form2(Form Form1Instance)
{
    this.Form1Reference = Form1Instance;
    InitializeComponent();
}

private void btnSAVE_Click(object sender, EventArgs e)
{
    this.Form1Reference.BackColor = btnKAMU.BackColor;
}

Or assign the Form1 reference to a private Field:

private Form Form1Reference;

public Form2(Form Form1Instance)
{
    this.Form1Reference = Form1Instance;
    InitializeComponent();
}

private void btnSAVE_Click(object sender, EventArgs e)
{
    this.Form1Reference.BackColor = btnKAMU.BackColor;
}

Depending on your context, it could be necessary to assing the chosen Color to a private Field and use its value to change Form1.BackColor

private Color Form1BackColor;

private void btnKAMU_Click(object sender, EventArgs e)
{
    colorDialog1.ShowDialog();
    btnKAMU.BackColor = colorDialog1.Color;
    this.Form1BackColor = btnKAMU.BackColor;
}

Change the previous code using this value if needed.

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Thank you, Jimi. But now I have another problem. Actually it is like this; When I click on btnSAVE, we need to create a new Txt file and register a 5 button background color. And when the other form is loaded, its background should be Txt color. Like this. Txt File: FORM1 [RED] | FORM2 [BLUE] | FORM3 [BLACK] and when form1 load, its background should be red, and if u can remember the colorDialog, when i choose a color and when i click btnSAVE, colors should be save in Txt file and when forms are loading backgrounds should be like in txt... – DoganalpK May 04 '18 at 06:34
  • @DoganalpK Well, that's a completly different question. You know how SO works: one question at the time. I can't answer it here. You have to post another question with this new requirement. You'll find answers quite quickly for this (as a hint, [start from this answer](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files?answertab=active#tab-top). If you get stuck, just ask SO, always showing what you have tried, doesn't matter if it's not working). – Jimi May 04 '18 at 06:44