|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article demonstrates how to read system colors and to color-in each row of a drop down list. In this example, I will show:
BackgroundI was asked to make an admin tool where different types of appointments can be set. These appointments would be of unique color and the admin could change the color of the appointment type at any time. I began to think of a drop down list where the name of the colors would be displayed and the background of that row would be of that color. With a view to that, I searched the web and couldn't find any solutions for a long time. Then I finally found a solution that seemed much more complex than needed, involving a database. So I tried to find an easier solution. Using the codeI am taking a drop down list control named <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>
At the server side, we need the following namespaces imported for further coding. using System;
using System.Web;
using System.Reflection;
using System.Drawing;
using System.Collections.Generic;
Let me show the protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
populateDdlMultiColor();
colorManipulation();
}
}
Let us now go through the private void populateDdlMultiColor()
{
ddlMultiColor.DataSource = finalColorList();
ddlMultiColor.DataBind();
}
Here goes the code for the 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;
}
However, there lies a problem with this approach. It includes system environment colors -- e.g. "Active Border" -- in the array. In order to resolve the problem, I extracted the system environment color. I used the Here, I filled the 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);
}
Each row of the drop down list is assigned the 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;");
}
Points of interestSo, we have learned how to get colors from History
|
||||||||||||||||||||||