Multi-color DropDownList using C# - 顏色挑選器

Multi-color DropDownList using C# - 顏色挑選器

參考: Multi-color DropDownList using C# By Fuad Bin Omar

之前在做案子的時候,我也有利用 DropDownList 抓取 System.Drawing.KnownColor 列舉型態,並配合 Enum.GetNames(typeof(KnownColor)) 抓取系統顏色清單,做成顏色挑選器供使用者挑選。但是卻沒有參考連結中,Fuad Bin Omar 寫得那麼完善,而且我當時也沒有考慮系統環境顏色必須忽略,例如【Active Border】。

幸好,今日在 MSDN 技術社群討論區 也有提到類似的問題,讓我搜尋到 Fuad Bin Omar 也有寫出這樣的顏色挑選器。剛剛,把 Fuad Bin Omar 的顏色挑選氣作一些調整,主要顯示顏色的部分加入 CSS,讓深色系的顏色用白色的文字來呈現顏色名稱;淺色系則使用黑色文字。比較如下:(右圖是修改後)

ColorPicker000 ColorPicker001

 

MultiColorDDlist.aspx 程式碼,如下:

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiColorDDlist.aspx.cs"
   2:      Inherits="MultiColorDDlist" %>
   3:   
   4:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   5:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   6:  <html xmlns="http://www.w3.org/1999/xhtml">
   7:  <head runat="server">
   8:      <title>Untitled Page</title>
   9:  </head>
  10:  <body>
  11:      <form id="form1" runat="server">
  12:      <div style="margin-left: 50px; margin-top: 50px;">
  13:          <table>
  14:              <tr>
  15:                  <td>
  16:                      <asp:DropDownList ID="ddlMultiColor" 
  17:                      OnSelectedIndexChanged="ddlMultiColor_OnSelectedIndexChanged"
  18:                          runat="server" AutoPostBack="true">
  19:                      </asp:DropDownList>
  20:                  </td>
  21:                  <td>
  22:                      <div id="msgColor" runat="server">
  23:                      </div>
  24:                  </td>
  25:              </tr>
  26:          </table>
  27:      </div>
  28:      </form>
  29:  </body>
  30:  </html>

 

 

MultiColorDDlist.aspx.cs 程式碼,如下:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Drawing;
   4:  using System.Reflection;
   5:  using System.Web.UI;
   6:   
   7:  public partial class MultiColorDDlist : Page
   8:  {
   9:      protected void Page_Load(object sender, EventArgs e)
  10:      {
  11:          if (!IsPostBack)
  12:          {
  13:              populateDdlMultiColor();
  14:              colorManipulation();
  15:          }
  16:      }
  17:   
  18:      protected void ddlMultiColor_OnSelectedIndexChanged(object sender, EventArgs e)
  19:      {
  20:          Color backColor = Color.FromName(ddlMultiColor.SelectedItem.Text);
  21:          ddlMultiColor.BackColor = backColor;
  22:          colorManipulation();
  23:          ddlMultiColor.Items.FindByValue(ddlMultiColor.SelectedValue).Selected = true;
  24:          string strStyle = string.Format("background:{0};width:30px;height:25px;",
  25:                                          ddlMultiColor.SelectedItem.Value);
  26:          msgColor.Attributes.Add("style", strStyle);
  27:      }
  28:   
  29:      private void colorManipulation()
  30:      {
  31:          for (int row = 0; row < ddlMultiColor.Items.Count - 1; row++)
  32:          {
  33:              Color backColor = Color.FromName(ddlMultiColor.Items[row].Value);
  34:              double grayValue = 0.299*backColor.R + 0.587*backColor.G + 0.114*backColor.B;
  35:              string strTextColor = (grayValue < 127.5) ? "white" : "black";
  36:              string strStyle = string.Format("background-color:{0}; color: {1};",
  37:                                              ddlMultiColor.Items[row].Value, strTextColor);
  38:   
  39:              ddlMultiColor.Items[row].Attributes.Add("style", strStyle);
  40:          }
  41:          ddlMultiColor.BackColor = Color.FromName(ddlMultiColor.SelectedItem.Text);
  42:      }
  43:   
  44:      private static List<string> finalColorList()
  45:      {
  46:          string[] allColors = Enum.GetNames(typeof (KnownColor));
  47:          string[] systemEnvironmentColors = new string[(typeof (SystemColors)).GetProperties().Length];
  48:   
  49:          int index = 0;
  50:          foreach (PropertyInfo member in (typeof (SystemColors)).GetProperties())
  51:          {
  52:              systemEnvironmentColors[index++] = member.Name;
  53:          }
  54:   
  55:          List<string> finalColorList = new List<string>();
  56:          foreach (string color in allColors)
  57:          {
  58:              if (Array.IndexOf(systemEnvironmentColors, color) < 0)
  59:              {
  60:                  finalColorList.Add(color);
  61:              }
  62:          }
  63:   
  64:          return finalColorList;
  65:      }
  66:   
  67:      private void populateDdlMultiColor()
  68:      {
  69:          ddlMultiColor.DataSource = finalColorList();
  70:          ddlMultiColor.DataBind();
  71:      }
  72:  }