C# - 跨表单使用 ColorDialog [英] C# - using ColorDialog across forms

查看:21
本文介绍了C# - 跨表单使用 ColorDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体应用程序.在主表单上,用户将输入多个项目等,然后单击将打开新表单的按钮(小表单或大表单,取决于复选框).现在在我的主应用程序上,我有一个文件菜单 - 在其下是设置 - 更改背景颜色.这将打开颜色对话框.如果用户没有选择任何东西,背景颜色将保持默认.但是,如果他们在主条目表单上更改它,我会更改一些文本框的背景 - 代码如下.

I have a windows form application. On the main form a user will enter a number of item, etc and then click a button which will open a new form (either a small form or a large form depending on a checkbox). Now on my main application I have a file menu - under which is settings - change background colour. This opens the colordialog. If a user does not pick anything the background colours will stay default. However if they change it on the main entry form i change the background of a few textboxes - code below.

private void warning1ToolStripMenuItem_Click(object sender, EventArgs e)
{
    colorDialog1.ShowDialog();
    Warn1Color = colorDialog1.Color.ToString();
    if (Warn1Color != null)
    {
        tbWarn1Hrs.BackColor = colorDialog1.Color;
        tbWarn1Mins.BackColor = colorDialog1.Color;
        tbWarn1Secs.BackColor = colorDialog1.Color;
        tbWarn1Msg.BackColor = colorDialog1.Color;
    }
}

现在我的问题是如何获得它,然后在打开的其他表单中更改背景.我希望我可以像处理其他一些值一样在新的表单构造函数中传递字符串.

Now my problem is how to I get this to then change the background in the other form that opens. I was hoping I could pass the string across in the new form constructor as i do with a number of other values.

即 - 这是我的新形式的代码....(注意 - 字符串 Warn1Color 在构造函数中传递,然后 = 到字符串 _Warn1Color.如果它为空,则背景将默认为黄色,但无法转换在 system.drawing.color 中输入字符串.有没有人看到一个简单的解决方案,或者我可以做些什么来轻松地完成这项工作.

i.e - here is my code in the new form....(note - string Warn1Color was passed across in constructor and then made = to the string _Warn1Color. If it is null then background will be default yellow but it cant convert type string to system.drawing.color. Does anyone see an easy solution to this or what I could do to get this working easily.

if (_Warn1Color == null)
{
    this.BackColor = System.Drawing.Color.Yellow;
}
else
    this.BackColor = _Warn1Color;

推荐答案

您应该创建一个静态类来存储您的配置数据,例如这种颜色样式.然后,您可以在提示用户进行更改后设置此值,也可以在需要使用它时从任何其他表单调用 Color 值.

You should create a static class to store your configuration data such as this colour style. You can then set this value once you have prompted the user for the change and you can also call the Color value from any other form when you need to use it.

你的静态类应该看起来像这样......

Your static class should look something like this...

public static class StyleSettings{
   private static Color _warn1Color = Color.FromArgb(255, 0, 0);//default colour
   public static Color Warn1Color {
      get { return _warn1Color; }
      set { _warn1Color = value; }
   }
}

然后您可以在示例方法中使用它,例如...

Then you can use this in your example method like...

private void warning1ToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (colorDialog1.ShowDialog() == DialogResult.OK)
    {
        StyleSettings.Warn1Color = colorDialog1.Color;

        tbWarn1Hrs.BackColor = StyleSettings.Warn1Color;
        tbWarn1Mins.BackColor = StyleSettings.Warn1Color;
        tbWarn1Secs.BackColor = StyleSettings.Warn1Color;
        tbWarn1Msg.BackColor = StyleSettings.Warn1Color;
    }
}

这篇关于C# - 跨表单使用 ColorDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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