Reader Level:
Articles

Getting All Colors in XNA

By Ibrahim Ersoy on Jul 08, 2012
I was playing with XNA today and was curious about if i can populate all the colors supported by XNA.For that i used Reflection method.
  • 0
  • 0
  • 4222
Download Files:
 

To display all the colors,i've used combobox from windows controls

First of all we need to get the Type of Color:

Type structType = typeof(Color);

Then we use PropertyInfo to get all the controls:

PropertyInfo[] fields = structType.GetProperties();

The reason why we didnt use FieldInfo instead of PropertyInfo is Each color is actually Property and have fields like R,G,B,A.

So we iterated through PropertyInfo and populated our combobox object.
 

foreach (PropertyInfo field in fields)

{

    cbo.Items.Add(field.Name);

}

When we run it,we'll be getting extra useless items:

1.png

So what to do?

I've written a simple iteration to remove these useless items:
 

for (int a = 0; a <= 5; a++)

{

    cbo.Items.RemoveAt(0);

}

It removes the first 6 items from the list.

Simple but useful.As it seems.

2.png

So?

Our work is done here.We got our Colors populated.

Hope it helps.

Ibrahim Ersoy

Ibrahim Ersoy lives in Turkey, Istanbul. He helps running and managing C# Corner. He is a Software Consultant (Sharepoint) and MindCracker MVP. He writes about cutting-edge Microsoft Technologies including XNA,... Read more

Personal Blog: http://www.iersoy.com
COMMENT USING