. NET reflection System. Drawing. Color struct,

Source: Internet
Author: User

. NET reflection System. Drawing. Color struct,

See the Code:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; // position of the Color struct; 6 using System. linq; 7 using System. text; 8 using System. threading. tasks; 9 using System. windows. forms; 10 using System. reflection; // Reflection must reference this namespace 11 12 namespace ReflectColorStructDemo13 {14 public partial class MainForm: Form15 {16 // form initialization 17 public MainForm () 18 {19 InitializeComponent (); 20 21 this. location = new Point (0, 0); // The Screen coordinate is 22 in the upper left corner of the form. this. size = SystemInformation. workingArea. size; // form Size, you need to modify the StartPosition attribute of the form to Manual, that is, manually 23 24 // the first time the form is loaded event processing method: Lambda syntax 25 this. load + = (object sender, EventArgs e) => 26 {27 Type type = typeof (Color); 28 PropertyInfo [] propertyInfo = type. getProperties (BindingFlags. public | BindingFlags. static); // obtain all the public and Static Color types. Property 29 30 int x, y, w, h, index; 31 32 x = 2; // initial x coordinate 33 y = 2; // initial y coordinate 34 w = 87; // The width of the Label control added to the Panel is 35 h = 65; // height 36 37 index = 0; // counter, the number of elements in the propertyInfo array is 38 39 int col = 15. // The number of columns is 40 int line = propertyInfo. length % col> 0? PropertyInfo. length/col + 1: propertyInfo. length/col; // number of rows 41 42 // the outer loop control row 43 for (int I = 0; I <line; I ++) 44 {45 // The inner loop control column 46 for (int j = 0; j <col & index <propertyInfo. length; j ++) 47 {48 Label label = new Label (); 49 label. location = new Point (x, y); 50 label. size = new Size (w, h); 51 52 label. backColor = Color. fromName (propertyInfo [index]. name); // set the Label control background color 53 54 label. text = string. concat (index + 1 ). toString (), '\ n', propertyInfo [index]. name); // explicit text 55 56 x + = w + 2; // x coordinate shift 57 index ++; // counter auto increment 58 59 this. panel1.Controls. add (label); // Add a new label to the Panel. Add a Panel control to the form and fill in the Form 60} 61 62 x = 2. // After the inner layer loops, return x coordinate 63 y + = h + 2; // y coordinate move down, 2 indicates control spacing 64} 65}; 66} 67} 68}

Paste execution:

 

 

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.