【问题标题】:Color the background of a form depending on radio button根据单选按钮为表单的背景着色
【发布时间】:2013-09-14 23:58:21
【问题描述】:

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

【问题讨论】:

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


【解决方案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);
    }
}

【讨论】:

  • 我认为委托部分是因为我正在尝试更改另一种形式而不是选项之一。
  • 这是目前正在做的事情。不过谢谢!我想我也不能这么问吧?
猜你喜欢
  • 2016-12-04
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
相关资源
最近更新 更多