Development Background:
Someone once asked me to develop a management tool that displays different colors based on different choices. I started to consider using the drop-down box to change the background and display color of the entries according to their names. Based on this idea, I searched the internet for a long time and did not find any related solutions, finally, I thought of a solution that is more complex than the original requirements (including databases), so I tried to find a simpler implementation solution.
This article mainly demonstrates how to read the system color and display the corresponding color in each entry in the drop-down box. The source code mainly displays the following content:
1. How to obtain the list enumeration of the system. Drawing. knowncolor Control
2. How to exclude the system environment color, such as "active border"
3. How to assign colors to each entry in the drop-down box
CodeDetails:
The name drop-down box is ddlmulticolor to display the color name and color. Use the <div> label to display the result of the Right rectangle. The aspx code is as follows:
< Table >
< Tr >
< TD >
< ASP: dropdownlist ID = "Ddlmulticolor"
Onselectedindexchanged = "Ddlmulticolor_onselectedindexchanged"
Runat = "Server" Autopostback = "True" >
</ ASP: dropdownlist >
</ TD >
< TD >
< Div ID = "Msgcolor" Runat = "Server" >
</ Div >
</ TD >
</ Tr >
</ Table >
In the CS file, we need to reference the following namespace:
Using System;
Using System. Web;
Using System. reflection;
Using System. drawing;
Using System. Collections. Generic;
Let's take a look at the page_load event first. In page_load, We will display the selected drop-down list.
Protected Void Page_load ( Object Sender, eventargs E)
{< br> If (page. ispostback = false )
{< br> populateddlmulticolor (); // 51aspx.com
colormanipulation ();
}
}
Now let's take a look at the populateddlmulticolor () function.
Private Void Populateddlmulticolor ()
{
Ddlmulticolor. datasource=Finalcolorlist ();
Ddlmulticolor. databind (); // liudao Translation
}
Finalcolorlist () method
Private List finalcolorlist ()
{
String [] Allcolors = Enum. getnames ( Typeof (System. Drawing. knowncolor ));
String [] Systemenvironmentcolors =
New String [(
Typeof (System. Drawing. systemcolors). getproperties (). Length];
Int Index = 0 ;
Foreach (Memberinfo Member In (
Typeof (System. Drawing. systemcolors). getproperties ())
{
Systemenvironmentcolors [Index++]=Member. Name;
}
List finalcolorlist = New List ();
Foreach ( String Color In Allcolors)
{
If (Array. indexof (systemenvironmentcolors, color) < 0 )
{
Finalcolorlist. Add (color );
}
}
Return Finalcolorlist;
}
System. Drawing. knowncolor is the color that comes with the Asp.net system. I have listed these colors through enumeration and bound them through finalcolorlist () correspondence. To implement this function, I used one of the most basic enumeration features: enum. getnames () sharing method. This method detects the enumerated content and outputs the result as a string sequence. Each value in the string corresponds to each result in the enumeration.
However, this method still has some problems. According to the above idea, the system environment color will be included in the enumeration amount, such as "active border (Note: active border)". To solve this problem, I expanded the system environment color. I used the system. reflection. memberinfo class.
Here, I use the system. Drawing. systemcolors attribute to fill in systemenvironmentcolors, and then create a list of images named finalcolorlist. In finalcolorlist, I only call known colors, but not system environment colors. Then, bind finalcolorlist to ddlmulticolor. So far, we have a drop-down box that includes all the color names. Let's take a look:
Private Void Colormanipulation ()
{
Int Row;
For (Row = 0 ; Row < Ddlmulticolor. Items. Count - 1 ; Row ++ )
{
Ddlmulticolor. items [row]. Attributes. Add ("Style",
"Background-color:" +Ddlmulticolor. items [row]. value );
}
Ddlmulticolor. backcolor =
Color. fromname (ddlmulticolor. selecteditem. Text); // liudao Translation
}
The style attribute of the background color of each row in the drop-down box corresponds to the color name displayed in the row. In the onselectedindexchanged event, the selected row in the drop-down box is highlighted using the <div> label combined with the following function, and the rectangle color on the right also changes.
Protected Void Ddlmulticolor_onselectedindexchanged ( Object Sender,
Eventargs E)
{
Ddlmulticolor. backcolor = Color. fromname (ddlmulticolor. selecteditem. Text );
Colormanipulation ();
Ddlmulticolor. Items. findbyvalue (ddlmulticolor. selectedvalue). Selected =
True ;
Msgcolor. Attributes. Add ( " Style " , " Background: " +
Ddlmulticolor. selecteditem. Value + " ; Width: 30px; Height: 25px; " );
}
So far, we have learned to get system. Drawing, export the system environment color, and bind the color name to the drop-down list.
Complete source code>
Original Author: Fuad Bin Omar Translation: liudao
Address: http://www.codeproject.com/aspnet/csMultiColorDropDownList.asp
Please criticize and correct the translation in this article!