How can I create an enumeration for colors in window forms application?
enumeration, vb.net
Solution
You can use an extension method. First simplify your enum:
enum ColorType
{
CompanyDarkBlue,
CompanyBlue,
CompanyLightBlue
}
The extension method will look something like this:
public static class ColorTypeExtensions
{
public static Color ToColor(this ColorType colorType)
{
switch(colorType)
{
case ColorType.CompanyDarkBlue: return Color.FromArgB(0,56,147);
...
}
}
}
This will allow you to write:
ColorType.CompanyDarkBlue.ToColor();
For more information take a look at C#: Enhance Enums using Extension Methods
Problem
I have come across a 'problem' that I think must be quite common and wondered if anybody could help. I am building a simple windows form (using VB.NET) for a friend to use at work. His company has about 10 specific colors schemes (they have list of RGB values) that they use for the company logo, website etc. I want to follow this color scheme in my application and, to simplify development, would like to build an enumeration of these colors to avoid hard coding the RBG value for every label, panel etc. My initial thought was to do the following: ``` Enum ColorTypes CompanyDarkBlue = Color.FromArgB(0,56,147) CompanyBlue = Color.FromArgB(0,111,198) CompanyLightBlue = Color.FromArgB(0,145,201) End Enum ``` However, it's not that simple as a constant is required. I looked around on the internet and I found an example of how to achieve what I need but it seemed inordinately complicated for what seems like quite a common requirement for application development. What do you think is the best way to solve this problem? Thanks much