Color the background of a form depending on radio button

5.8k views Asked by At

I have been working on having my form change the background color immediately depending on the option a user selects. He can choose between red, green and blue. I was trying to use the system.drawing.color but I canĀ“t seem to make the form change it's color. It is supposed to use delegates.

I am just learning about modeless dialog boxes. Help please...

So far this is whatI have done:

      public partial class ChangeColors : Form
{
    //Delegate the observer needs to notify if something changes
    public delegate void ChangeColorEvent(Color nameColor);
    // Name of the event which is tied to the delegate
    public event ChangeColorEvent ChangeColor;

    public enum color { Red, Green, Blue };
    private color selectedColor;

    public color SelectedColor
    {
        get
        {
            return selectedColor;
        }
        set
        {
            selectedColor = value;
        }
    }

    public ChangeColors()
    {
        InitializeComponent();
        selectedColor = color.Blue;
    }

    private void backColor_ColorChanged(Control control)
    {
        if (redRadioButton.Checked)
            this.BackColor = System.Drawing.Color.Red;
        else if (blueRadioButton.Checked)
            this.BackColor = System.Drawing.Color.Blue;
        else
            this.BackColor = System.Drawing.Color.Green;
    }

    private void okButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
1

There are 1 answers

2
AsfK On

I not know how to do this with delgete but with events is the code:

public partial class Form1 : Form
{
    private void backColor_ColorChanged(object sender, EventArgs e)
    {
        if (redRadioButton.Checked)
            this.BackColor = System.Drawing.Color.Red;
        else if (blueRadioButton.Checked)
            this.BackColor = System.Drawing.Color.Blue;
        else if (greenRadioButton.Checked)
            this.BackColor = System.Drawing.Color.Green;
    }

    public Form1()
    {
        InitializeComponent();
        redRadioButton.CheckedChanged += new EventHandler(backColor_ColorChanged);
        blueRadioButton.CheckedChanged += new EventHandler(backColor_ColorChanged);
        greenRadioButton.CheckedChanged += new EventHandler(backColor_ColorChanged);
    }
}