Click here to Skip to main content
15,888,351 members
Articles / Drawing
Tip/Trick

Show your System.Drawing.Color Palette

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Nov 2012CPOL 8.4K   4  
How to show your System.Drawing.Color palette

Introduction

If you try to write a program and then you want to add code to highlight text by color, you will go and use System.Drawing.Color. But I will ask you a question. Do you know all colors in this class to set an appropriate color. This program will help you to create a palette that contains all colors in this class with its name so it will be useful to have an appropriate color and take its name.

Using the Code

This code loops through all properties of color class by using Reflection and then trying to get its name and value and putting it in grid cell:

VB.NET
DataGridView1.Rows.Add()

Dim cellcount As Int16 = 1
Dim rowcount As Int16 = 0
Dim COLORs As System.Drawing.Color

For Each p As System.Reflection.PropertyInfo In COLORs.GetType().GetProperties()
    Dim modval As Int16
    
    If p.CanRead Then
        ' Dim OBJ As System.Drawing.Color
        Dim objType As Type = COLORs.GetType()
        Dim pInfo As System.Reflection.PropertyInfo = objType.GetProperty(p.Name)
        Dim PropValue As Object = pInfo.GetValue_
        (COLORs, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
        If TypeOf (PropValue) Is System.Drawing.Color Then
            modval = cellcount Mod 5
            
            If modval = 0 Then
                modval = 5
            End If
            
            Dim r As Int16 = DataGridView1.Rows.GetFirstRow_
            	(DataGridViewElementStates.Displayed)
            CallByName(DataGridView1.Item(modval - 1, rowcount).Style, _
            	"BackColor", CallType.Set, PropValue)
            CallByName(DataGridView1.Item(modval - 1, rowcount), _
            	"value", CallType.Set, p.Name)
            
            If modval = 5 Then
                DataGridView1.Rows.Add()
                rowcount = rowcount + 1
            End If
            
            cellcount += 1
        End If
    End If
Next 

Please provide feedback about my code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --