Rahul Soni's blog

Never assume the obvious is true!

Color Chart code which enumerates all the names of the .NET "Color Structure"

Color Chart code which enumerates all the names of the .NET "Color Structure"

  • Comments 1

It often becomes difficult to remember the color names in .NET. Now, if you have a datagrid or any other control and looking for appropriate colors, this code will help you to create a small page which will show you all the colors in one page, along with their names. It uses .NET reflection to get names of all the Color Names in the .NET Color structure.

Here is what the output looks like...

ColorChart.JPG

Let's create a new web page in a VB.NET Web project called ColorChart and delete everything in the code behind. Then, paste the following code and run the project with ColorChart as the start page.

Imports System.Reflection
'
Public Class ColorChart
    Inherits System.Web.UI.Page
    '
#Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    End Sub
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub
#End Region
    '
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '
        'Put user code to initialize the page here
        Response.Write("<Table width='100%' border=1 cellspacing=0 cellpadding=0>")
        Response.Write("<TR bgcolor='gray'><TD width='16%'><B>Color Name</TD><TD width='16%'>" & _
        "<B>Color</TD><TD width='16%'><B>Color Name</TD><TD width='16%'><B>Color</TD>" & _
        "<TD width='16%'><B>Color Name</TD><TD width='16%'><B>Color</TD></TR>")
        '
        'This is because we want to get the assembly's information
        Dim x As Color
        Dim strType As Type = x.GetType()
        Dim arrMemberInfo() As MemberInfo = strType.GetProperties
        Dim myMemberInfo As MemberInfo
        'Lets Enumerate the colors now in three columns
        Dim i As Integer = 0
        Dim Remainder As Integer = 0
        For Each myMemberInfo In arrMemberInfo
            Remainder = i Mod 3
            If Remainder = 0 Then
                Response.Write("<TR>")
            End If
            Response.Write("<TD>" + myMemberInfo.Name + "</TD>")
            Response.Write("<TD bgcolor='" + myMemberInfo.Name + _
            "' borderColor='black' borderColorLight='black' borderColorDark='black'></TD>")
            '
            If Remainder = 2 Then
                Response.Write("</TR>")
            End If
            i += 1
            If myMemberInfo.Name = "YellowGreen" Then Exit For
        Next myMemberInfo
        Response.Write("</Table>")
    End Sub
End Class

Hope that helps!

Attachment: ColorChart.JPG
  • Rahul Soni did a god job and sent a short code to generate .NET colors in a webpage in ASP.NET 1.x. It...
Page 1 of 1 (1 items)
Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post