开发者

C# - using ColorDialog across forms

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 ba开发者_JAVA技巧ckground 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.

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;


Pass the Color on via the Constructor not a string. If this is not possibly for whatever reason, you could create a ColorConfigClass that holds the required Color and you can set it when used.


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;
    }
}


I assume you used a string because you wanted to be able to pass null, and System.Drawing.Color being a struct can not be null.

In which case either use Nullable ( http://msdn.microsoft.com/en-us/library/b3h38hb0%28v=vs.80%29.aspx ) which can be null or you can consider some other value as "default", say alpha=0.

To pass a value in your constructor simply go to the code file for the form (the one where you code the stuff for the events) and find the constructor function (has the same name as the form) e.g.:

namespace MyApp
{
    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
        }
        ...

And add the parameters to it:

namespace MyApp
{
    public partial class MyForm : Form
    {
        public MyForm(System.drawing.color background)
        {
            InitializeComponent();
            ...do whatever you want with background...
        }
        ...

Of course you also need to edit the places you create this form, e.g. change

form = new MyForm();
form.Show();

to

form = new MyForm(backgroundColour);
form.Show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜