C # color of the programming enumeration system-programming tool-original

Source: Internet
Author: User

In system development, system colors are often used to modify your own controls.

If you do not have the help of an artist, choose the appropriate system color in what you see and what you get from your aesthetic point of view.

Below we will use a piece of code to implement it.

First, define a form, which may be called FrmColors. In Text, we can enter Enum Colors. Then add a FlowLayoutPanel control to FORM.

The function of this control is to automatically display the generated control in the form of a list.

I personally prefer to add the InitThisForm METHOD TO THE FORM constructor to initialize the Form Control. Of course, Microsoft officially does not recommend this. It is just as you get used to it.

The color of enumeration systems must use the following aspects:

1. Use the typeof operator;

2. Use KnownColor for enumeration;

3. dynamically generate controls.

The following shows the main code of the tool:

 1         private void InitThisForm()
2 {
3 // Get all the values from the KnownColor enumeration.
4   System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
5 KnownColor[] allColors = new KnownColor[colorsArray.Length];
6
7 for (int i = 0; i < colorsArray.Length; i++)
8 {
9 string colorName = colorsArray.GetValue(i).ToString();
10 // KnownColor kc = colorsArray.GetValue(i) as KnownColor;
11   Color color = Color.FromName(colorName);
12
13 string controlName = "lbl_" + i.ToString();
14 Label lbl = new Label();
15 lbl.Name = controlName;
16
17 lbl.BackColor = color;
18 lbl.Text = colorName;
19
20 lbl.ForeColor = color.R < 128 || color.G < 128 || color.B < 128 ? Color.White : Color.Black;
21 lbl.Click += new EventHandler(lb_Click);
22 lbl.MouseHover += new EventHandler(lb_MouseHover);
23 lbl.MouseLeave += new EventHandler(lb_MouseLeave);
24 lbl.MouseEnter += new EventHandler(lbl_MouseEnter);
25 this.flowLayoutPanel1.Controls.Add(lbl);
26
27 Label lbsplit = new Label();
28 lbsplit.AutoSize = false;
29 lbsplit.Height = 1;
30 this.flowLayoutPanel1.Controls.Add(lbsplit);
31 }

Now, you can use the BackColor of the Label created by the program to display the system color and the ColorName in TEXT.

We noticed that the Click event was added when the Label was created to display the Color name in txtColorName when clicking the Label.

The Code is as follows:

1         void lb_Click(object sender, EventArgs e)
2 {
3 this.lblSelectColor.BackColor = ((Label)sender).BackColor;
4 this.txtColorName.Text = ((Label)sender).BackColor.Name;
5 this.txtColorName.SelectAll();
6 }

In the MouseEnter and MouseLeave events, I changed the BorderStyle attribute of the Label to display the labels for better highlighting.

 1         void lbl_MouseEnter(object sender, EventArgs e)
2 {
3 Label lb = (Label)sender;
4 lb.BorderStyle = BorderStyle.FixedSingle;
5 }
6
7 void lb_MouseLeave(object sender, EventArgs e)
8 {
9 Label lb = (Label)sender;
10 lb.BorderStyle = BorderStyle.None;
11 }

In addition, the parameter this.txt ColorName. SelectAll () is added to the clickevent code. When you click an ideal color, you can directly use Ctrl + C to set the color.

The name is copied to the clipboard.

As follows:


Related Article

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.