I'm trying to fill my list with system.drwaing.color items to pick a random color and set it to backColor.
Here is my code:
    List<Color> myList = new List<Color>();
    //rc.Add(Color.Chartreuse);
    //rc.Add(Color.DeepSkyBlue);
    //rc.Add(Color.MediumPurple);
    foreach (Color clr in System.Drawing.Color)
    {
      //error  
    }
    Random random = new Random();
    Color color = myList[random.Next(myList.Count - 1)];
    this.BackColor = color;
Error: 'System.Drawing.Color' is a 'type', which is not valid in the given context
Can anyone give me a Hand?
public static List<Color> ColorStructToList()
{
    return typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public)
                        .Select(c => (Color)c.GetValue(null, null))
                        .ToList();
}
List<Color> colorList = ColorStructToList();
private void randomBackgroundColorButton_Click(object sender, EventArgs e)
{
    List<Color> myList = ColorStructToList();
    Random random = new Random();
    Color color = myList[random.Next(myList.Count - 1)];
    this.BackColor = color;
}
public static List<Color> ColorStructToList()
{
    return typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public)
                        .Select(c => (Color)c.GetValue(null, null))
                        .ToList();
}
From this question:
string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With