Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Show your System.Drawing.Color Palette

0.00/5 (No votes)
24 Nov 2012 1  
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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here