Thursday, October 21, 2010

PropertyGrid (.NET C#)

The topic today is simply the PropertyGrid. While to many this is nothing new nor very exciting. But if you have been writing applications without knowledge of the PropertyGrid control now is the time. Go forth and investigate. The PropertyGrid allows you to assign an object and not only view but edit the properties of an object. It makes debugging and tweaking incredibly easy. There are some disadvantages though. You cannot cycle through values, instead you must commit each change by pressing enter or leaving the control. Below is an example of a property exposed from an object with just the bare essential properties. The description to display in the PropertyGrid for the Thickness property is defined with the DescriptionAttribute.


[DescriptionAttribute("Border Thickness (0 is fill)")]
public int Thickness
{
get { return m_nThickness; }
set { m_nThickness = value; }
}


You will need to do a little research on the various attributes you can apply to your properties. Some basics are: 
  • CategoryAttribute
  • DescriptionAttribute 
  • BrowsableAttribute 
  • ReadOnlyAttribute 
  • DefaultValueAttribute 
  • DefaultPropertyAttribute
To assign an object for viewing/editing just assign your PropertyGrid.SelectedObject value the desired object. There are of course events associated with the PropertyGrid as well so you can catch value changes. The only application I have used a PropertyGrid for so far is my Card Maker application. I will definitely consider it in any future projects or those I revisit.

1 comment: