C # traverse all colors and names under System. drawing. Color to view

Source: Internet
Author: User

During the interview, I was asked how to traverse all the colors and names under System. drawing. Color to view them. When the answer was poor, I recorded the solution as follows:

Copy codeThe Code is as follows: 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 button#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) // only take the known Color in the attribute and remove the byte attribute and some Boolean attributes (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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.