Reputation: 9017
In visual studio, when creating controls in the markup(or in code-behind) you can specify colors in HEX format like this: "#FFFFFF", but you also can select from the list of preset colors like: White, Wheat, Window, etc. (See screenshot).
Is it possible to extend that list and add additional colors?
Upvotes: 12
Views: 3641
Reputation: 3404
This list is based on System.Drawing.KnownColor
enum values. As enums cannot be extended and you cannot inherit from another enum, if you want to create list with more named colors to use in your own code you have to implement your own enum with those values added manually and with additional ones you wish to see there.
And, of course, along with System.Drawing.KnownColor
enum there is a method in Color
struct that is used to get the value of this named color - Color.FromKnownColor
. I think you should also add extention method to Color
struct that will do the same for your own enum, as FromKnownColor
method does for KnownColor
enum. And maybe you can even use FromKnownColor
method in your own extension method to make it simplier for colors that already exist in this standard enum.
But if the only thing you want to do is to extend this one list in ASP.NET designer, you cannot do this with any changes in your own project or by changing options in VS. Maybe you can write an add-in to Visual Studio to do this, but that's the only way I see, if it's what you wanted to do.
EDIT: If inheriting the control is an option for you, maybe the best answer you can get is the one suggested by simplecoder, as described in his answer - creating your own control based on the old one with this new enum values binded to the property you want to use new colors for.
Upvotes: 16
Reputation: 7341
You really can't edit this list provided by .Net Framework to add additional colors of your choice. The reasons are quite obvious as summarized in the above post by @Konrad Gadzina. You can choose the alternate way as suggested by @simplecoder.
But I would rather say, use the Pick Color functionality to grab your color of your choice rather going through all these hard way.
Upvotes: 0
Reputation: 5815
As @Sayse pointed out you can not create extension property : why ? Read this :MSDN Blog one way of doing is creating extesion method and using it. which @Sayse pointed @Konrad pointed the issue with KnownColor Enum
So here is a solution with little twist.
By Inheriting Control class , i am positive this should work. This involve little work , but once you get hang of it it's very straight forward.
Override the BackColor Property , BackColor is defined in Parent class of Button which is
System.Web.Ui.WebControls.WebControl
Make the BackColor Set property to Private and Link it to BackColor 2
if you need existing color , then copy them your new enum. using reflector
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
namespace ClassLibrary1
{
public class MyButton : System.Web.UI.WebControls.Button
{
[DefaultValue(typeof(Color), "")]
public virtual Color BackColor
{
get
{
if (!this.ControlStyleCreated)
return Color.Empty;
else
return this.ControlStyle.BackColor;
}
private set
{
// this.ControlStyle.BackColor = value; , OVerride it with your MyColor
this.ControlStyle.BackColor =YourMethodConvertMyColorToColor(this.BackColor2);
}
}
private Color YourMethodConvertMyColorToColor(MyColor c){
//your implementation
if (c == MyColor.MyColorGreen)
{
return ColorTranslator.FromHtml("#FF0000"); ; //just example
}
return Color.Green;//default
}
MyColor _mycolor; //Global Value
[DefaultValue(typeof(MyColor), "")]
public MyColor BackColor2
{
get
{
return _mycolor;
}
set
{
this._mycolor = value;
}
}
}
public enum MyColor
{
MyColorGreen=1,
MyColorBlue=2
}
}
Edit : System.Drawing.Color is Structure so it can not be Inherited. This is show stopper. That's why i thought inheriting control is only a solution
Also I tried to add a extension Method like this and it did not work for me. In designer i do not see myColor
public static class MyExtensions
{
public static System.Drawing.Color MyColor(this System.Drawing.Color c)
{
return Color.AliceBlue; // could be your color
}
}
Upvotes: 7
Reputation: 2733
you can create your own drop down list containing all the colors from the already available list.
Dim colorList as new DropDownList()
Dim colorArray As String() = System.Enum.GetNames(GetType(KnownColor))
colorList.DataSource = colorArray
colorList.DataBind()
now you can add your own manual colors to the list.
colorList.Items.Add(newcolor.ToString)
ofcourse to use it inside the Visual Studio IDE you have to create your own Visual Studio Add-in.
Upvotes: 0
Reputation:
You need to write your own control and add design time support for Visual Studio. This can allow you to use it directly from the VS Toolbox. For quick solution consider defining the color in code behind using this code :
using System.Drawing;
this.myButton.BackColor = ColorTranslator.FromHtml("#FF0000");
Upvotes: 3
Reputation: 43300
Not directly as you cannot create extension properties.
One solution is to make an extension method
public static Color MyNewColor(this Color str)
{
return Color.FromArgb(255, 255, 255, 255);
}
This can then be called from
(new Color()).MyNewColor()
so slightly different and probably less efficient as it is creating two colors instead of one like the existing properties, but will achieve what you are looking for..
Upvotes: 3