1

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

1 回答 1

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);
    }
}
于 2013-09-11T05:42:44.777 回答