Click here to Skip to main content
15,881,687 members
Articles / Protected

Building a color selector control

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Oct 2013CPOL1 min read 6.3K   1  
This article will show you how to create a color selector control that will looks like the image in the right side.   The first thing to mention

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This article will show you how to create a color selector control that will looks like the image in the right side.  Colors Selector Image

The first thing to mention is the way that we are going to fill the colors. We can manually fill the available colors or we can find a way to get all the known colors using .NET.

We will use the values of System.Drawing.KnownColor Enum type to fill the colors list. This can be done using the Enum.GetValues method:

C#
System.Drawing.KnownColor[] colors =
 (System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor));

For a better usability, all the code logic can be placed in a usercontrol, so that you can use it in many places in your project.

UserControl Code :

ASP
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="Select color" Value="-1"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DropDownList1"
    runat="server" ErrorMessage="
Select a color from the list !" InitialValue="-1">
</asp:RequiredFieldValidator> 
 
C#
public partial class ColorsPicker : System.Web.UI.UserControl
{
    public System.Drawing.Color SelectedColor
    {
        get {
            if (DropDownList1.SelectedIndex > 0)
            {
                //get the selected color 
                string SelectedColorStr = DropDownList1.SelectedValue;
                //' conver the color string to System.drawing.color object 
                System.Drawing.ColorConverter converter = new System.Drawing.ColorConverter();
                System.Drawing.Color objColor = 
                    (System.Drawing.Color)converter.ConvertFromString(SelectedColorStr);
                return objColor;
            }
            // if there is no color selected 
            //  we return empty color object
            else
            {
                return  new System.Drawing.Color();
            }
        }
        set {
            string Color = value.Name;
            ListItem li = DropDownList1.Items.FindByValue(Color);
            // if found in the colors list , we select the matched color 
            if (li != null)
            {
                DropDownList1.ClearSelection();
                li.Selected = true;
            }
        }
    
    }
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
            BindFillColorslist();
        //set the Background color of the listitem to the same color which the item represents
        SetCbackColorsForListItems();
    }
    private void SetCbackColorsForListItems()
    {
        foreach (ListItem li in DropDownList1.Items)
        {
            li.Attributes["style"] = " background-color: " + li.Value;
        }
    }
    // this function will fill the dropdownlist with the known colors 
    private void BindFillColorslist()
    {
        // get the list of known colors 
        System.Drawing.KnownColor[] colors =
            (System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor));
        //' add them to the drodownlist 
        foreach (System.Drawing.KnownColor c in colors)
        {
            DropDownList1.Items.Add(c.ToString());
        }
    }
}

Some Important notes on the UserControl code :

  1. The SelectedColor property can be used to set/get the selected color in the colors list, this property accepts an object of type System.Drawing.Color .
  2. The SetCbackColorsForListItems function is responsible for setting the background color for each item based on the Color that is represented by the item.
  3. The BindFillColorslist is used to populate  the Colors List.

This is an example on how to use the control in your page

ASP
<uc1:ColorsPicker ID="ColorsPicker1" runat="server" />
<asp:Button ID="Button2" runat="server" Text="show color" OnClick="Button2_Click" />
<asp:Label ID="lblColor" runat="server">Color Test</asp:Label></div>
C#
protected void Button2_Click(object sender, EventArgs e)
    {  // set the label back color based on the selected date from the list 
        lblColor.BackColor = ColorsPicker1.SelectedColor;
    }

Now when you click on the button, the label background color will be set based on the selected color from the colors list.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --