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 ();
}
}