Saturday, January 29, 2011

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.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (System.Reflection.PropertyInfo prop in typeof(Color).GetProperties())
{
if (prop.PropertyType.FullName == "System.Drawing.Color")
comboBox1.Items.Add(prop.Name);
}
}
}
}

No comments:

Post a Comment

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...