Let the PropertyGrid control display property descriptions and categories in C#

The example Use a PropertyGrid control to let the user edit program objects in C#shows how to use the PropertyGrid control to let the user view and edit simple object properties.

You can use property attributes to give editors such as the PropertyGrid extra information about an object's properties. For example, you can give the properties a description and a category that the editor should use to list the properties.

In the picture on the right, the Person object's properties are grouped into the Address, Contact Info, and Name categories. The picture also shows the description for the currently selected property, Email.

To use these attributes, include the System.ComponentModel namespace in a using statement.

using System.ComponentModel;

Next place the Description and Category attributes before the properties. The following code shows the FirstName property's definition with these attributes.

private string _FirstName;
[Description("The person's first or given name")]
[Category("Name")]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}

Now the PropertyGrid can automatically group properties by category and display their descriptions.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.