Development background:
Someone once asked me to develop a management tool that would display different colors based on different selections. I began to consider using drop-down boxes to change the background and display color of items based on different item names. Based on this idea, I searched online for a long time and could not find any relevant solutions. Finally, I thought of a solution that was more complicated than what I originally wanted. solution (including database), so I tried to find a simpler implementation.
This article mainly demonstrates how to read system colors and display the corresponding color in each entry in the drop-down box. The source code mainly shows the following content:
1. How to obtain the list enumeration of the System.Drawing.KnownColor color control
2. How to exclude system environment colors, such as "Active Border"
3. How to assign colors to each item in the drop-down box
Detailed code explanation:
Name the drop-down box ddlMultiColor to display the color name and color. Use the <div> tag to display the rectangular result on the right. 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 process and display the selected drop-down list.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
populateDdlMultiColor(); //51aspx.com
colorManipulation();
}
}
Now let us take a look at 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 itself. I have listed these colors through enumeration and bound them through finalColorList() correspondence. In order to achieve this function, I used one of the most basic enumeration features: the Enum.GetNames() shared method, which detects the enumeration content and outputs the result as a string sequence, and each value in the string corresponds to Each result in the enumeration.
However, there are some problems with this approach. According to the above idea, the enumeration amount will include the system environment color, such as "Active Border (Note: Active Border)". In order to solve this problem, I expanded the system environment color. I used System.Reflection.MemberInfo class.
Here I fill systemEnvironmentColors with the System.Drawing.SystemColors property, and then create a graphics list named finalColorList. In finalColorList, I only call known colors, but not in the system environment colors. Then bind finalColorList to ddlMultiColor. At this point, we already have a drop-down box containing all color names. Let's do it:
http://www.downcodes.com/
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 through the following function combined with the <div> tag, and the color of the rectangle on the right also changes accordingly.
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 how to get System.Drawing and discharge the system environment color, and bind the color name to the drop-down list.