Reader Level:
Articles

How to Load all Colors in a ComboBox Using C#

By Mahesh Chand on Jan 04, 2011
This tip loads all colors an ComboBox control.
  • 0
  • 0
  • 19513

Today, I was working on a Windows application where I need to display listing of all colors in a ComboBox and selection of the color sets the background color of a Form.

In Windows Forms, the Color structure has a public property for each color. We can use System.Reflection to read all colors of a Color structure and load its properties.

On my Form, I have a ColorComboBox control. The following code adds all colors to the ComboBox.


foreach
(System.Reflection.PropertyInfo prop in typeof(Color).GetProperties())
{   
     if (prop.PropertyType.FullName == "System.Drawing.Color"
)
             ColorComboBox.Items.Add(prop.Name);
}


Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more

COMMENT USING

Trending up