When you work on designing a website, you may find it difficult to remember all the color names and visualize them. At least, I am personally not very good with color selection and find it really hard to recall the color names. So, in this scrap we will check out how to use .NET reflection to print out all the color names along with the colors...
There is a very small piece of C# code which I have used here to create a 2 column color table.
Check the sample
And here is the code that would do it for you...
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ OutputCache Duration="72000" VaryByParam="none" Location="Any" %>
<script runat="server">
protected void form1_Load(object sender, EventArgs e)
{
Response.Write("<a href='http://www.dotnetscraps.com'><img alt='.NET Scraps'" +
"src='http://www.dotnetscraps.com/pics/dotnetscraps-logo.gif' style='border:0' /></a>");
//Put user code to initialize the page here
Response.Write("<Table width='100%' border=1 cellspacing=3 cellpadding=3>");
Response.Write("<TR bgcolor='LightCyan'><TD><B>Color Name</TD><TD width='30%'>" +
"<B>Color</TD><TD><B>Color Name</TD><TD width='30%'><B>Color</TD>");
//Get the assembly's information
Color x = new Color();
Type strType = x.GetType();
MemberInfo[] arrMemberInfo = strType.GetProperties();
//Lets Enumerate the colors now in three columns
int i=0;
int Remainder = 0;
foreach (MemberInfo myMemberInfo in arrMemberInfo)
{
Remainder = i % 2;
if(Remainder == 0)
Response.Write("<TR>");
Response.Write("<TD>" + myMemberInfo.Name + "</TD>");
Response.Write("<TD bgcolor='" + myMemberInfo.Name +
"' borderColor='black' borderColorLight='black' borderColorDark='black'></TD>");
if (Remainder == 1)
Response.Write("</TR>");
i += 1;
if (myMemberInfo.Name == "YellowGreen")
break;
}
Response.Write("</Table>");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Color Sample 1</title>
</head>
<body style ="font-family:verdana">
<form id="form1" runat="server" onload="form1_Load">
</form>
</body>
</html>
Here is the output... (Check the live sample)
Hope that helps
Rahul