Search code examples
c#gdi+systemcolors

List all SystemColors


In Windows.Forms, I need to create a program which accepts any color and tries to find the corresponding system colors to it.

I was not able to figure out how to loop through all Colors of the System.Drawing.SystemColors class - it's a class, not an enum or a List.

How can I do this (some kind of reflection?)?


Solution

  • How about

    public static Dictionary<string,object> GetStaticPropertyBag(Type t)
    {
        const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
    
        var map = new Dictionary<string, object>();
        foreach (var prop in t.GetProperties(flags))
        {
            map[prop.Name] = prop.GetValue(null, null);
        }
        return map;
    }
    

    or

    foreach (System.Reflection.PropertyInfo prop in typeof(SystemColors).GetProperties())
    {
         if (prop.PropertyType.FullName == "System.Drawing.Color")
             ColorComboBox.Items.Add(prop.Name);
    }