0

I have a form let's call it formMain that has a menu which opens another form called formColors. Now when formColors opens it has radio buttons labeled with Colors i.e Red

How can I change the font color of formMain to whichever option I choose in formColors?

0

1 Answer 1

2

Create an Event in formColors and subscribe to it in formMain or as Hans Passant pointed out you could use a public property and the Forms DialogResult Property this would be the preferred way to do it.

WinForm DialogResult Example

Form1

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

    private void button1_Click(object sender, EventArgs e)
    {
        frm2 = new Form2();
        if (frm2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            this.BackColor = frm2.formColor;

        frm2.Close();   
    }
}

Form2

public partial class Form2 : Form
{
    Color newColor;
    public Form2()
    {
        InitializeComponent();
    }
    public Color formColor
    {
        get { return this.newColor; }
        set { this.newColor = value; }
    }

    private void btnRed_Click(object sender, EventArgs e)
    {
        newColor = Color.Red;
    }

    private void btnBlue_Click(object sender, EventArgs e)
    {
        newColor = Color.Blue;
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        DialogResult = System.Windows.Forms.DialogResult.OK;
    } 

}

WinForm Event Example:

Form1:

public partial class Form1 : Form
{
    Form2 frm2 = new Form2();
    public Form1()
    {
        InitializeComponent();
        frm2.ColorEvent += new ColorEventHandler(frm2_ColorEvent);

    }

    void frm2_ColorEvent(object sender, ColorEventArgs e)
    {
        this.BackColor = e.formColor;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm2.ShowDialog();
    }
}

Form2:

public delegate void ColorEventHandler(object sender, ColorEventArgs e); 
public partial class Form2 : Form
{
    public event ColorEventHandler ColorEvent;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ColorEventArgs newColor = new ColorEventArgs();
        newColor.formColor=Color.Red;
        ColorEvent(this, newColor);
    }


}

ColorEventClass

public class ColorEventArgs : EventArgs
{
    private Color newColor;
    public Color formColor
    {
        get { return this.newColor; }

        set { this.newColor = value; }
    }
}
4
  • This isn't correct, a dialog should have no effect when the user cancels it. Apr 7, 2012 at 7:22
  • @HansPassant The dialog has no effect on the parent form when canceled, only when the button is clicked to raise the new event.
    – Mark Hall
    Apr 7, 2012 at 15:27
  • So if the user clicks the button, then cancels the dialog it is still changed. This should not be an event, just a public property. And if ShowDialog() returns DialogResult.Ok then use the property. Apr 7, 2012 at 15:30
  • @HansPassant That would be easier, I will edit answer to show it, I was just trying to give a quick example of a custom event.
    – Mark Hall
    Apr 7, 2012 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.