How to change control properties in Form1 from Form2?

Hello,

I have Form1 - the main form with some controls including a button.

I have Form2 - the properties form with a combobox and button. The user can select the colors of a button in the combobox in Form2 and press "OK". What code should I add to "OK" so that the user can change the colors of a button in Form1

Mateusz


Answer this question

How to change control properties in Form1 from Form2?

  • mtnanhu

    Form2 should expose a property that gives you the color selected, then if the user pressed OK, Form1 should read this and set the color of the buttons with it.

    I hope your forms are not named Form1 and Form2 :-)


  • Keaven

    Can you please explain more on how to do it. I am just a C# beginner.
  • PremK

    In Form2:


    public Color NewColor
    {
      get { return _newColor; }
    }

    private Color _newColor;

    This variable is set within the dialog, by the end user selecting a color.  Actually, there's a color dialog built in to Windows, are you using that, or how is a color being selected

    In Form1

    Form2 dlg = new Form2;

    if (DialogResult.OK == dlg.ShowDialog())
    {
       this.theButton.BackColor = dlg.NewColor;
    }

    I'm going from memory, the property may not be called BackColor, but that's the general idea.


  • How to change control properties in Form1 from Form2?