How to generate random color names in C#

后端 未结 15 1791
清酒与你
清酒与你 2020-11-29 21:57

I need to generate random color names e.g. \"Red\", \"White\" etc. How can I do it? I am able to generate random color like this:

Random randonGen = new Ran         


        
15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 22:51

    Copied code from Retrieve a list of colors in C#

    CODE:

    private List GetColors()
    {
        //create a generic list of strings
        List colors = new List();
        //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;
    }
    

提交回复
热议问题