Display Color Hex Values in a DropDownList

D

Dave

How can I populate a DropDownList with the Available Color Hex Values
while displaying the Color? I'd like to allow my users a way to set a
color profile.

Thanks
 
D

Dave

Sorry Patrice, this is a web page. My intention is to display the
color as forecolor with the hex value as the value of the dropdown.
 
P

Patrice

Sorry Patrice, this is a web page. My intention is to display the
color as forecolor with the hex value as the value of the dropdown.

Could give something such as :

Color[] colors={System.Drawing.Color.Red,System.Drawing.Color.Green};
foreach(Color color in colors)
{
ListItem li=new ListItem();
li.Attributes.Add("style","color:#"+String.Format("{0:x}",color.ToArgb()).Substring(2));
li.Text=String.Format("{0:X}",color.ToArgb()).Substring(2);
li.Value = li.Text;
ddl.Items.Add(li);
}

ddl being an ASP.NET DropDownList...

You can use the a 0:X placeholder to convert a value to an hexa value (I use
Substring to keep only RGB values and drop the Alpha component). Using the
Attributes collection, you can add the appropriate style to set the color
even if the ListItem class has no explicit properties for this...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top