In vs. net, we can easily use the Properties window to set the properties of a control. Have you ever thought about it? Program When setting the custom properties in the program, a form like the property window is displayed. Can you set the properties conveniently? As shown in.
The answer is yes. We can use the property control provided by Microsoft to implement this function. First, create a C # windows application, right-click the toolbox, and select "Add/Remove items" in the pop-up menu ", as shown in:
In the pop-up window, select the. NET freamwork components window, select the property grid control, and click Select to add the control, as shown in:
Now we can start using this control. The first step is to create a public attribute for the field to be displayed in the application. All properties must have get and set methods. (If the get method is not set, the properties to be displayed will not be displayed in the Property Control ). To set relevant properties, you must set the following attributes about the property control, as shown in the following table:
| Attribute Value |
Description |
| Categoryattribute |
This attribute sorts attributes in the Property Control alphabetically. |
| Descriptionattribute |
The value is the specific text description of each property, which is displayed at the bottom of the property control. |
| Browsableattribute |
Whether to display or hide an attribute in the Property Control |
| Readonlyattribute |
Whether a property value is read-only in the Property Control |
| Defaultvalueattribute |
Default value of each attribute |
Next, we create a user class and use the property control to change its value in the property control box. First, we will introduce the relevant namespaces:
| Using system. componentmodel; |
Then, create related classes and set relevant attributes,CodeAs follows:
/// Customer class to be displayed in the property grid /// </Summary> // [defaultpropertyattribute ("name")] Public Class Customer { Private string _ name; Private int _ age; Private datetime _ dateofbirth; Private string _ SSN; Private string _ address; Private string _ email; Private bool _ frequentbuyer; [Categoryattribute ("ID settings"), descriptionattribute ("Name of the customer")] public string name { Get { Return _ name; } Set { _ Name = value; } } [Categoryattribute ("ID settings"), descriptionattribute ("Social Security Number of the customer")] Public String SSN { Get { Return _ SSN; } Set { _ SSN = value; } } [Categoryattribute ("ID settings"), descriptionattribute ("Address of the customer")] Public String address { Get { Return _ address; } Set { _ Address = value; } } [Categoryattribute ("ID settings"), descriptionattribute ("date of birth of the customer (optional)")] Public datetime dateofbirth { Get {return _ dateofbirth ;} Set {_ dateofbirth = value ;} } [Categoryattribute ("ID settings"), descriptionattribute ("Age of the customer")] Public int age { Get {return _ age ;} Set {_ age = value ;} } [Categoryattribute ("marketting Settings"), descriptionattribute ("if the customer has bought more than 10 times, this is set to true")] Public bool frequentbuyer { Get {return _ frequentbuyer ;} Set {_ frequentbuyer = value ;} } [Categoryattribute ("marketting Settings"), descriptionattribute ("most current e-mail of the customer")] Public String email { Get {return _ email ;} Set {_ email = value ;} } Public customer (){} } |
As you can see, in the above Code, we have set attributes in the customer class, such as name, date of birth, and address.
Next, we will create an instance for the created customer class and bind it to the Property Control. The Property Control automatically displays relevant properties in the interface based on the settings of the attributes in the class, and can also be edited. For example, you can modify the birthday attribute, the calendar control box is displayed during modification, which is very convenient. The Code is as follows:
Private void form1_load (Object sender, system. eventargs E) { // Create a bill object and instantiate the customer class Customer bill = new customer (); // Assign a value to the attribute Bill. Age = 50; Bill. Address = "114 Maple Drive "; Bill. dateofbirth = convert. todatetime ("9/14/78 "); Bill. SSN = "123-345-3566 "; Bill. Email = bill@aol.com" Bill. Name = "Bill Smith "; // Bind the object to the Property Control Propertygrid1.selectedobject = bill; } |
Finally, run the program and we will get the results illustrated in the beginning of this article. Let's review this program. We use the catrgoryattribute attribute and define ID settings and marketsettings. They appear in the attribute control in a classification form (note that they have a "+" number before them, click to show its subattributes ). At the same time, when selecting an attribute, the description of this attribute is displayed at the lower part of the attribute control box.
The Property Control has many advantages. This article only gives a brief introduction to it, hoping to inspire readers.