WinForm the use of form PropertyGrid controls

Source: Internet
Author: User

You have used Microsoft Visual Basic or Microsoft Visual Studio. NET friends, you must use the property browser to browse, view, or edit the properties of one or more objects. The. NET Framework PropertyGrid controls are at the heart of the Visual Studio. Net Property Browser. The PropertyGrid control displays the properties of an object or type and retrieves the properties of the item primarily by using reflection, which provides the type information at run time. The PropertyGrid control contains the following sections: General properties, expandable properties, property categories, property descriptions, and property editors.

Below, I introduced the use of PropertyGrid controls in the form of code, based on my previous blog, using DockPanel (referencing WeifenLuo.WinFormsUI.Docking.dll components) in WinForm.

First, define a PropertyGrid control and add it to the Dockpanelright form

// Add a PropertyGrid control to a dockpanelleft form New PropertyGrid ();d ockPanelSideRight.Controls.Add (PropertyGrid); Initpropertygrid (); Private void Initpropertygrid () {    new Point (00);     // propertygrid.anchor = anchorstyles.left;    Propertygrid.dock = DockStyle.Fill;}

Next, add an attribute class UserProperty, and customize the properties, and the attribute class UserProperty code is as follows:

classuserproperty{Private string_appname; Private string_apppath; PrivatePoint _location;//= new Point (0, 0);    PrivateSize _size =NewSize (0,0); PrivateColor _backcolor; PrivateColor _forecolor; PrivateFont _font; PrivateString _text; [CategoryAttribute ("General"), DescriptionAttribute ("Application Name"), ReadOnlyAttribute (true)]     Public stringAppName {Get        {            return_appname; }        Set{_appname=value; }} [CategoryAttribute ("General"), DescriptionAttribute ("Application Path"), ReadOnlyAttribute (true)]     Public stringAppPath {Get        {            return_apppath; }        Set{_apppath=value; }} [CategoryAttribute ("Layout"), DescriptionAttribute ("location"), ReadOnlyAttribute (false)]     PublicPoint location {Get        {            return_location; }        Set{_location=value; }} [CategoryAttribute ("Layout"), DescriptionAttribute ("Dimensions"), ReadOnlyAttribute (false)]     Publicsize Size {Get        {            return_size; }        Set{_size=value; }} [CategoryAttribute ("appearance"), DescriptionAttribute ("Background Color"), ReadOnlyAttribute (false)]     PublicColor BackColor {Get        {            return_backcolor; }        Set{_backcolor=value; }} [CategoryAttribute ("appearance"), DescriptionAttribute ("Front View"), ReadOnlyAttribute (false)]     PublicColor ForeColor {Get        {            return_forecolor; }        Set{_forecolor=value; }} [CategoryAttribute ("appearance"), DescriptionAttribute ("text")]     PublicString Text {Get        {            return_text; }        Set{_text=value; }} [CategoryAttribute ("appearance"), DescriptionAttribute ("Font")]     PublicFont Font {Get        {            return_font; }        Set{_font=value; }    }}

Finally, instantiate the UserProperty class and set it to the Selectedobject property of PropertyGrid

// set the value of the PropertyGrid control, custom properties New  = propertyuser;initpropertyuser ();   Private void Initpropertyuser () {    this. Text;      This . ClientSize;      This . Location;}

The effect of the operation is as follows:

  

To change the way some properties are displayed, you can apply different attributes to those properties. Attributes are declarative tokens used to annotate programming elements such as types, fields, methods, and properties, which can be retrieved at run time using reflection. As shown below:

    • The DescriptionAttribute setting displays the property text in the Help pane below the properties. This is an efficient way to provide help text for an activity property (that is, a property with focus).
    • CategoryAttribute sets the category that the property belongs to in the grid. This feature is useful when you need to group properties by category name. If no category is specified for the attribute, the property is assigned to the miscellaneous category.
    • BrowsableAttribute indicates whether the property is displayed in the grid. This attribute can be used to hide properties in the grid. By default, public properties are always displayed in the grid.
    • ReadOnlyAttribute indicates whether the property is read-only. This attribute can be used to disallow editing of properties in the grid. By default, public properties with get and set access functions can be edited in the grid.
    • DefaultValueAttribute represents the default value for the property. Use this attribute if you want to provide a default value for the property, and then determine whether the property value is the same as the default value. You can apply this attribute to all properties.
    • Defaultpropertyattribute represents the default property of the class. When you select a class in the grid, the default properties of the class are highlighted first.

WinForm the use of form PropertyGrid controls

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.