Thursday, January 27, 2011

How to load all colors in a ComboBox using C#

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);
}


Ref. Link: - http://www.c-sharpcorner.com/UploadFile/mahesh/4925/Default.aspx

No comments: