c traverses all the colors and names below System.drawing.Color to see

  • 2020-05-09 19:05:18
  • OfStack

During the interview, I was asked how to go through all the colors and names under System.drawing.Color to check.


View Code 
     public partial class Form1 : Form
     {
         FlowLayoutPanel newPanel = new FlowLayoutPanel();

         public Form1()
         {
             InitializeComponent();
             newPanel.AutoScroll = true;
             //newPanel.FlowDirection = FlowDirection.BottomUp;
             //newPanel.WrapContents = false;
             newPanel.Dock = DockStyle.Fill;
             newPanel.BackColor = Color.White;
             button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

         }

         private void button1_Click(object sender, EventArgs e)
         {
             newPanel.Controls.Clear();
             int i = 1;

             foreach (var item in typeof(Color).GetMembers())
             {
                 if (item.MemberType == System.Reflection.MemberTypes.Property && System.Drawing.Color.FromName(item.Name).IsKnownColor == true)// Takes only the property and is known in the property Color , eliminate byte Properties, and 1 Some Boolean properties, etc. ( A B G R IsKnownColor Name Etc.) 
                 {
                     Label myLable = new Label();
                     myLable.AutoSize = true;

                     myLable.BackColor = System.Drawing.Color.FromName(item.Name);
                     myLable.Text = System.Drawing.Color.FromName(item.Name).Name;
                     newPanel.Controls.Add(myLable);
                     //newPanel.GetFlowBreak(myLable);

                     i++;
                 }
             }

 
             this.Controls.Add(newPanel);
             button1.Text = i.ToString();
         }
     }


Related articles: