根据单选按钮为表单的背景着色

时间:2013-09-11 05:09:26

标签: c# radio-button system.drawing.color

我一直在努力让我的表单立即根据用户选择的选项更改背景颜色。他可以选择红色,绿色和蓝色。我试图使用system.drawing.color,但我似乎无法使表单改变它的颜色。它应该使用代表。

我正在学习无模式对话框。请帮忙......

到目前为止,这就是我所做的:

      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 个答案:

答案 0 :(得分:1)

我不知道如何使用delgete执行此操作,但事件是代码:

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);
    }
}