Home>

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 shows the following:

1.How to get a list enumeration of system.drawing.knowncolor color controls

2. How to exclude the color of the system environment,Like "active border"

3.How to assign color to each item of the drop-down box

Detailed code:

Name the drop-down box ddlmulticolor to display the color name and color,Display the rectangular result on the right with the

tag,aspx code is as follows

<table>
 <tr>
 <td>
 <asp:dropdownlist id="ddlmulticolor"
 onselectedindexchanged="ddlmulticolor_onselectedindexchanged"
 runat="server" autopostback="true">
 </asp:dropdownlist>
 </td>
 <td>
 <div runat="server">
 </div>
 </td>
 </tr>
 </table>

In the cs file we need to reference the following namespaces:

using system;
 using system.web;
 using system.reflection;
 using system.drawing;
 using system.collections.generic;

Let's first look at the page_load event. In page_load, we process the selected drop-down list.

protected void page_load (object sender, eventargs e)
 {
 if (page.ispostback == false)
 {
 populateddlmulticolor ();//51aspx.com
 colormanipulation ();
 }
 }

Now let's take a look at the populateddlmulticolor () function

private void populateddlmulticolor ()
 {
 ddlmulticolor.datasource=finalcolorlist ();
 ddlmulticolor.databind ();//
 }
 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 by enumeration and bound them via the finalcolorlist () correspondence.To achieve this function,I used one of the most basic enumeration features:the enum.getnames () shared method,This method detects the enumeration content and outputs the result as a sequence of strings.Each value in the string corresponds to each result in the enumeration.

However, this method is still somewhat problematic.Following the above idea,The enumeration amount will include the system environment color.For example, "active border", in order to solve this problem,I expanded the system environment colors.I used the system.reflection.memberinfo class.

Here I fill the 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 color.Then bind the finalcolorlist to ddlmulticolor. At this point, we have a drop-down box that includes all color names.Let's do this:

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 that row.The selected row in the drop-down box in the onselectedindexchanged event is highlighted with the

tag through the following function,At the same time, the color of the rectangle 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;");
 }

At this point, we learned that if we get system.drawing and emit the system environment color,And bind the color name to the drop-down list.

  • Previous Detailed paging query for SQLServer and Oracle
  • Next Summary of PHP 9 big cache technology