如何使用组合框中的值更改表单的背面颜色? [英] How do I change the Back color of a form using values in a combo box?

查看:57
本文介绍了如何使用组合框中的值更改表单的背面颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个员工查看器应用程序,我的应用程序中有多个表单。我希望用户可以选择将主窗体的背面颜色更改为(红色,绿色,洋红色和蓝色),然后再返回其默认颜色。主窗体包含一个组合框,其中的值如下:



默认

红色

绿色

洋红色

蓝色



当用户点击这些值时,我希望表单更改背面颜色。这样做的最佳方法是什么?提前谢谢你。

解决方案

嗨Leandro,下面你发现我在评论中写的代码是什么,它适用于一种形式。

MyApplicationSettings类位于底部,否则表单设计者会抱怨。如果您需要多表单解决方案,则必须为MyApplicationSettings实例提供一个全局的生存位置。然后Form2到Formn将订阅BackColorChanged事件并提供他们的OnBackColorChanged事件处理程序。





公共部分类Form1:Form 
{
public MyApplicationSettings ApplicationSettings = new MyApplicationSettings();

public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

ApplicationSettings.BackColorChanged + = OnBackColorChanged;
comboBoxFormBackColor.SelectedIndexChanged + = OnComboBoxFormBackColorChanged;
comboBoxFormBackColor.Items.Add(System.Drawing.Color.Red);
comboBoxFormBackColor.Items.Add(System.Drawing.Color.Blue);
comboBoxFormBackColor.Items.Add(System.Drawing.Color.Green);
comboBoxFormBackColor.Items.Add(System.Drawing.Color.Magenta);
comboBoxFormBackColor.SelectedIndex = 0;
ApplicationSettings.FormBackColor =(System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
}

private void OnComboBoxFormBackColorChanged(object sender,EventArgs e)
{
ApplicationSettings.FormBackColor =(System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
}

private void OnBackColorChanged(object sender,EventArgs e)
{
this.BackColor = ApplicationSettings.FormBackColor;
}

}


公共类MyApplicationSettings
{
private Color _formBackColor;
public System.Drawing.Color FormBackColor
{
get
{
return _formBackColor;
}
设置
{
_formBackColor = value;
OnBackColorChanged();
}
}

公共事件EventHandler BackColorChanged;
protected void OnBackColorChanged()
{
EventHandler handler = BackColorChanged;
if(handler!= null)handler(this,EventArgs.Empty);
}

}


我指的是你发布的解决方案:

不要使用ComboBox.Click-Event作为你的方法。

更好地使用ComboBox.SelectedIndexChanged。



但对我来说,来自FranzBe的解决方案,作为评论发布,应该是最好的...


我认为你可以为用户颜色维护一个表,如(UID和ClorName)默认你可以设置任何一个颜色一个用户更改然后您可以将新颜色保存到表中以供将来请求。





如果它对您有帮助,请标记为ans。

I am creating an employee viewer application, i have multiples forms in my applications. I want users to have the option of changing the back color of the Main form to (Red, Green, Magenta and Blue) for example, and then back to its default color. The main form contains a combo box with the values in it like:

Default
Red
Green
Magenta
Blue

I want the form to change the back color when a user clicks each of these values. What is the best approach to do it? Thank you in advance.

解决方案

Hi Leandro, below you find what I wrote in the comment as code, it works for one form.
The MyApplicationSettings class is at the bottom, otherwise the forms designer will complain. If you want the multi-form solution, you have to give the MyApplicationSettings instance some global place to live. Form2 to Formn will then subscribe to the BackColorChanged event and provide their OnBackColorChanged event handler.


public partial class Form1 : Form
{
  public MyApplicationSettings ApplicationSettings = new MyApplicationSettings();

  public Form1()
  {
    InitializeComponent();
  }

  protected override void OnLoad(EventArgs e)
  {
    base.OnLoad(e);

    ApplicationSettings.BackColorChanged += OnBackColorChanged;
    comboBoxFormBackColor.SelectedIndexChanged += OnComboBoxFormBackColorChanged;
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Red);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Blue);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Green);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Magenta);
    comboBoxFormBackColor.SelectedIndex = 0;
    ApplicationSettings.FormBackColor = (System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
  }

  private void OnComboBoxFormBackColorChanged(object sender, EventArgs e)
  {
    ApplicationSettings.FormBackColor = (System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
  }

  private void OnBackColorChanged(object sender, EventArgs e)
  {
    this.BackColor = ApplicationSettings.FormBackColor;
  }

}


public class MyApplicationSettings
{
  private Color _formBackColor;
  public System.Drawing.Color FormBackColor
  {
    get
    {
      return _formBackColor;
    }
    set
    {
      _formBackColor = value;
      OnBackColorChanged();
    }
  }

  public event EventHandler BackColorChanged;
  protected void OnBackColorChanged()
  {
    EventHandler handler = BackColorChanged;
    if (handler != null) handler(this, EventArgs.Empty);
  }

}


I refer to your posted solution :
Don't use the ComboBox.Click-Event for your method.
Better use the ComboBox.SelectedIndexChanged.

But for me, the solution from FranzBe, posted as comment, should be the best one ...


i think you can maintain one table for user color like( UID and ClorName) for default you can set any color one a user change then you can save new color into table for future request.


please mark as ans if it helps you.


这篇关于如何使用组合框中的值更改表单的背面颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆