Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add color options to System.Drawings.Color

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?
enter image description here

like image 221
user194076 Avatar asked Apr 24 '13 21:04

user194076


2 Answers

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.

like image 142
Konrad Gadzina Avatar answered Sep 20 '22 21:09

Konrad Gadzina


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.

  1. Inherit Control class e.g MyCustomButton inherits System.web.UI.WebControls.Button
  2. Create a new Property BackColor2 , Which consume as New Enum of MyColor
  3. Override the BackColor Property , BackColor is defined in Parent class of Button which is
    System.Web.Ui.WebControls.WebControl

  4. 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

enter image description here

enter image description here

    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
        }
    }  
like image 33
aked Avatar answered Sep 24 '22 21:09

aked