1 Replies - 1998 Views - Last Post: 08 August 2012 - 08:13 PM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Retrieve a list of colors in C#

Posted 07 November 2007 - 07:56 PM

Description: Make sure to have:

using System.Collections.Generic;
using System.Drawing;This snippet takes advantage of the new List< > available to us in C# 2.0 to populate a list of all known colors

 private List<string> GetColors()
    {
        //create a generic list of strings
        List<string> colors = new List<string>();
        //get the color names from the Known color enum
        string[] colorNames = Enum.GetNames(typeof(KnownColor));
        //iterate thru each string in the colorNames array
        foreach (string colorName in colorNames)
        {
            //cast the colorName into a KnownColor
            KnownColor knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
            //check if the knownColor variable is a System color
            if (knownColor > KnownColor.Transparent)
            {
                //add it to our list
                colors.Add(colorName);
            }
        }
        //return the color list
        return colors;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve a list of colors in C#

#2 viiron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 08-August 12

Re: Retrieve a list of colors in C#

Posted 08 August 2012 - 08:13 PM

A much simple way to go about this is as follows: string[] colorNames = System.Enum.GetNames(typeof(System.Drawing.KnownColor)); comboBox4.Items.AddRange(colorNames); Assuming of course you wanted to put the results in a combobox.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1