views:

200

answers:

4

I'm building a WinForms application, and I'm using specific colors on most controls. I do that sometimes from the WinForms designer, and sometimes directly into my code.

I got a static class somewhere looking like that :

 public static class MyColors
    {
        public static Color DarkGreen = Color.FromArgb(0, 70, 62);
        ...
        public static Color Orange = Color.FromArgb(239, 132, 16);

    }

I can use those colors in my code quite easily, but it's impossible to do from the designer, which raises this error :

MyColors.DarkGreen is not a valid value for Int32.

(I've tried to store the Int32 representation of those colors, but this fails with the same error)

The solution I'm using right now is to use the rgb color code in the designer, the MyColors class values in my code, and I'm doing changes using the replace all functionality of Visual Studio. This isn't a nice solution, but I haven't been able to find a better idea so far.

Any ideas ?


Note : I know about this question, which is slightly different from mine, as I'm not looking at changing the "KnownColors".

+1  A: 

To get designer support for your additional color definitions, you probably need to create your own editors that extend or replace the Color editor. Once you have defined these, you then would need to tell the designer which properties should use your editor by applying the Editor attribute to the affected properties.

You may also be able to achieve some success by doing something similar with a TypeConverter and the TypeConverterAttribute.

Jeff Yates
@Jeff : since I want to update the colors of default winforms controls (button, label, etc), I can't really apply the Editor attribute to their ForeColor/BackColor properties, unless I inherit from all those controls, which sounds a bit overkill to me ... or did I miss something ?
Brann
@Brann: You already inherit by implementing your form. You'd have to apply it to overrides within your own form.
Jeff Yates
Of course, the controls within the form would either need to be set to Color.Empty to inherit from their parent control, or you'd need to override those too. I don't believe there is an alternative to get designer support, I'm afraid.
Jeff Yates
@Jeff : I inherit by implementing my form, BUT I don't implement the controls I'm dragging over it. Also, I don't want all my controls to have the same backcolor and/or forecolor (ie I want to be able to create a darkgreen button and an orange button on the same form), which means I can't inherit (not in the oo meaning) the colors from the parent control (ie the form)
Brann
@Brann: Yes, as I said, I think you'd have to derive each control type to add the designer support for your additional color definitions.
Jeff Yates
+2  A: 

I don't think this is possible, because you would need to change the behavior of the Properties window itself. Typing in "255, 128, 64" works because the Properties window contains code that tries to turn whatever is typed into a system color, a named "web" color, or else an RGB-specified color, and shows the "blah blah blah is not a valid value for Int32." message for any text that it can't convert thusly (like "MyColors.DarkGreen").

MusiGenesis
You can override this behavior by providing your own TypeConverter, but you need to override the controls to add that to the properties for it to work.
Jeff Yates
@Jeff: I believe you, and as is typical for StackOverflow, I get votes for saying "you can't do this" while you get no votes (until mine) for suggesting how you can do this. :)
MusiGenesis
A: 

Your best bet is to probably not try to do anything in the designer. Just leave all of your forms with the default colors at design time.

I've been on projects that had a scheme similar to yours and all sorts of problems crept up. A button got set to an RGB value in the designer; Another button on the same form got it's color set in the code (from the static class) and the two colors were slightly different. Over time these problems will creep in.

If you ever want to change a color, you'll have to change the static class and check all of your forms. Subtle differences will not be obvious.

If you set all of your colors in the code, if you miss a control, it will immediately be obvious. If you decide to change a color, you only need to change it in one place.

Another tweak that we did was to add members to the static class that had a usage mentioned in their name.

For example:

 public static class MyColors
    {
        public static Color DarkGreen = Color.FromArgb(0, 70, 62);
        ...
        public static Color Orange = Color.FromArgb(239, 132, 16);

        public static Color ButtonBackColor = DarkGreen;
    }

This way, if you want to change all of your buttons from DarkGreen to Orange, it's only a one line change.

NascarEd
A: 

This should give you the result you are looking for. There are probably excessive number of brackets that you can remove.

public static Color DarkGreen = Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(70)))), ((int)(((byte)(62)))));
sdolphin