How to Load all Colors in a ComboBox Using C#

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.
  1. foreach (System.Reflection.PropertyInfo prop in typeof(Color).GetProperties())  
  2. {     
  3.      if (prop.PropertyType.FullName == "System.Drawing.Color")  
  4.              ColorComboBox.Items.Add(prop.Name);  
  5. }  
Next>> ComboBox in C#


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.