Go from original text in C # using property controls to add Properties window
The first step is to create a field property that will be presented in the application as public. Where all properties must have a method of get and set (if the Get method is not set, the properties to be displayed are not displayed in the property control). To set the related properties, you must set some of the following property values about the property control, as shown in the following table:
| Property value |
Meaning |
| CategoryAttribute |
This property categorizes the properties in the property control in alphabetical order |
| DescriptionAttribute |
The value is a specific textual description of each property, which will be displayed at the bottom of the properties control |
| BrowsableAttribute |
The value is whether to show or hide a property in the properties control |
| ReadOnlyAttribute |
This value is a property value that is read-only in the properties control |
| DefaultValueAttribute |
The default value for each property |
Next, we create a user class and use the property control to make it possible to change its value in the Property control box. Let's introduce the relevant namespaces first:
| Using System.ComponentModel; |
After that, create the related classes, set the relevant properties, and the code is as follows:
Customer class to being 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 times, this is SE T 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 set the properties in the customer class, such as name, date of birth, address, and so on.
Next, we create an instance of the customer class that we created and bind it to the property control. The property control automatically displays the relevant properties in the interface based on the related settings of the property in the class, and can also be edited, for example, to make changes to the Birthday property and to pop up the Calendar control box when it is modified. The code is as follows:
private void Form1_Load (object sender, System.EventArgs e) { Create a Bill object, instantiate the customer class Customer Bill = new Customer (); assigning to attributes Bill. Age = 50; Bill. Address = "Maple Drive"; Bill. dateOfBirth = Convert.todatetime ("9/14/78"); Bill. SSN = "123-345-3566"; Bill. email = "[Email protected]" Bill. Name = "Bill Smith"; To bind an object to a property control Propertygrid1.selectedobject = Bill; } |
Finally, running the program, we get the results from the beginning of this article. Review the program, where we used the Catrgoryattribute property, which defines IDs settings and Marketsettings, which appear as categories in the attribute controls (note that they have a "+" sign before they can be expanded to see their child properties). At the same time, each time we select a property, the description of the property is displayed at the bottom of the Property control box.
Property control has a lot of advantages, this article is just a brief introduction, hoping to inspire readers.
To add a property window using a property control in C #