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

本文关键字:背景 窗体 单选按钮 | 更新日期: 2023-09-27 18:30:56

我一直在努力让我的表单根据用户选择的选项立即更改背景颜色。他可以在红色,绿色和蓝色之间进行选择。我试图使用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();
    }
}

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

我不知道

如何使用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);
    }
}