C # Using the Property Grid Control Learning Notes (1)

zhaozj2021-02-16  54

1. Add the Property Grid control to the toolbox

Since the Property Grid control is not displayed in the toolbox by default, you need to be added manually.

Figure 1 Add the Property Grid to the Toolbox (right-click on the toolbox) Add / remove items)

Figure 2 Select the PropertyGrid control in the Custom Toolbox dialog box

2. Write a custom class and bind the PropertyGrid control.

Using system; using system.componentmodel;

Namespace propertyGridDemo {[DefaultPropertyAttribute ("name")] public class customer {private string name; private string email; private string maark;

[CategoryAttribute ("User Information"), DescriptionAttribute ("Setting Consumer Name")] Public String Name {Get {Return Name;} Set {Name = Value;}}

[CATEGORYATTRIBUTE ("User Information"), DescriptionAttribute ("Setting Consumers Email Address)] Public String Email {Get {Return Email;} Set {email = value;}}

[CategoryAttribute ("Remarks"), DescriptionAttribute ("Remark Information"] PUBLIC STRING Mark {Get {Return Mark;}}}}}}}}}}}}}}

First define the custom type, you want to reference the System.comPonentModel namespace, and some Attribute classes in the namespace will be used, and DEFAULTPROPERTYATTRIBUTE, CATEGORYATTRIBUTE, and DESCRIPTIONATTRIBUTE are mainly used in the examples.

DefaultPropertyAttribute Specifies the default properties of the component. CategoryAttribute specifies the name of the category for the attribute or event group when the property or event is displayed in the System.Windows.Forms.PropertyGrid control set to be set to the SYSTEM.WORMS.PROPERTYGRID control in classification.

DescriptionAttribute Specifies an instructions for attributes or events.

When writing a custom class, all attributes should have a GET and SET methods. If this property is not displayed in the propertyGrid, if there is no set method, the read-only property cannot set the value of this property in the PropertyGrid.

Bind the custom class with the PropertyGrid control

Bind using the SELECTEDObject property of the PropertyGrid class

Private Void Form1_Load (Object Sender, System.EventArgs E) {Customer Customer = New Customer (); Customer.Name = "Zhang San"; Customer.email = "zhangsan@sina.com";

PropertyGrid1.selectedObject = Customer;}

operation result:

转载请注明原文地址:https://www.9cbs.com/read-18928.html

New Post(0)