Click here to Skip to main content
11,709,366 members (63,523 online)
Click here to Skip to main content

A fully customizable PropertyGrid

, 24 May 2007 CPOL 130.7K 7.1K 116
Rate this:
Please Sign up or sign in to vote.
A grid to display and edit properties of objects (as in Visual Studio)

Screenshot - PropertyGrid.jpg

Introduction

This article implements a fully customizable property grid as can be seen in the above screenshots. The grid supports various types of items: strings, combos, file pickers... It also supports fully customizable items that can define how they are drawn, how the user interacts with them and their behavior when edited.

Background

As developers, we are now all used to the Properties Window of Visual Studio and this article is an implementation of this. It does not support the help area and although the control supports different types of display (categorized, alphabetical...) standard buttons for this are not provided.

Using the code

The sample application provided shows almost all that can be done with the control. You are therefore encouraged to parse the source code to learn about the control!

First we have placed a dummy control in our dialog (in our case a Picture Control) and overridden the type of the control in the header file to be a CPropertyGrid. In the OnInitDialog of the dialog, we initialize the controls using various methods:

  • AddSection: used to add a group of properties (see "Basic Items" of the screenshot)
  • AddXXXItem: used to add items to a section (see below for the various items types)
  • SetXXXStrings: used to localize the standard dialogs used by the CPropertyGrid (see below)

Basically, if the only thing we want to do is to display standard properties, then we are done!

Standard item types

As said previously, the control supports different items, so here is a quick description of those types:

  • AddStringItem: a simple single line string item. The string can be edited in-place
  • AddTextItem: a multiline string item. When edited, CPropertyGrid dynamically generates a dialog allowing the user to enter the text
  • AddIntegerItem: an integer value item. An optional formatting mask can be provided
  • AddDoubleItem: a floating point value item. An optional formatting mask can be provided
  • AddComboItem: an item allowing to choose among a set of values. The possible values are passed as a vector<string>
  • AddBoolItem: a boolean value item which behaves like a Combo Item. True and False texts can be set using SetTrueFalseStrings
  • AddDateItem: a date value item chosen using a CMonthCalCtrl derived control.
  • AddDateTimeItem: a date and time value set using a dynamically generated dialog
  • AddFileItem: an item allowing to choose a file using a standard CFileDialog. A filter list can be provided
  • AddFolderItem: an item allowing to choose a folder using a "standard" directory picker dialog
  • AddColorItem: an item allowing to choose a color using a standard CColorDialog
  • AddFontItem: an item allowing to choose a color using a standard CFontDialog

Custom items

CPropertyGrid also supports full custom items. To implement a custom item, simply create a new class and make it derive from ICustomItem and implement the various methods of this interface. Then create an instance of your class and add it the grid control using AddCustomItem. Two methods of ICustomItem are purely virtual and must be overridden:

  • GetEditMode: tells the grid what kind of interaction your control is using when editing the value. The sample application shows four custom items that implement the various edit modes.
  • DrawItem: override this to draw your custom item. This can be as simple as calling DrawText on the provided CDC.

Next, depending on the edit mode, you may override more methods to customize your item. To better understand how this works it is recommended to check the various custom items implemented in the sample application: CSerialItem for in-place editing, CRectItem for modal editing, CTreeItem for dropdown editing and CGradientItem for a full custom item.

  • In-place edited items should override GetStringForInPlaceEdit (to tell the grid the string that should be edited) and OnItemEdited (return false to discard the edit)
  • Dropdown custom items should override ShowDropDown and show their dropdown control here
  • Modal custom items should override OnEditItem and show their modal dialog here
  • Custom edit mode items should override OnLButtonDown, OnMouseMove and OnLButtonUp to implement their mouse events specific handling

Display customization

Rendering of the grid can also be customized using various methods:

  • Title shading: control if you want to have a different background color for section headers
  • Draw lines: control if you want to display horizontal grid lines or not
  • Gutter: control if you want to display the vertical line between property names and values. You can also control the width of the gutter
  • Focus disabled: control if you want disabled items to be selectable or not
  • Colors: use the various SetXXXColor methods to change the colors used to draw the grid

Value editing

You can use the GetItemValue and SetItemValue overloads to get the value of a specific item. The HITEM expected is one returned when you called AddXXXItem when initializing your control. Make sure you call the good overload when calling this function: obviously you cannot get double from date item and you cannot set a LOGFONT in a text item. That's why all these functions return a boolean value indicating if things went well or not.

Points of Interest

I must warn you that this is some code that I wrote a while back and that has not changed much since. I always wanted to put this one on Codeproject but obviously did not find time to do it. Now it's done!

I only provided project files for Visual Studio 2005 but I see no reason as to why the code should not work with 2003 or even 6.0. Maybe some minor adjustments should be done but it should not be a big issue.

I am using two other articles available on codeproject.com:

Conclusion

This was a fun control to write especially supporting full custom items. My description of the control does not cover all aspects of the control and I encourage you to review its header file to discover things that I have not taken the time to talk about. Nevertheless, I hope that this short article let you see all the possibilities of the control!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nicolas Bonamy
Team Leader
France France
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionis there anybody to use this source code in VS2013? Pin
Member 117513409-Aug-15 3:30
memberMember 117513409-Aug-15 3:30 
Generali wonder Pin
Member 464780015-Sep-14 17:07
memberMember 464780015-Sep-14 17:07 
Questionchild of child Pin
hujik27-Feb-14 21:38
memberhujik27-Feb-14 21:38 
GeneralPossible improvements Pin
Siddharth Hegde25-Jan-13 3:37
memberSiddharth Hegde25-Jan-13 3:37 
GeneralRe: Possible improvements Pin
hujik27-Feb-14 21:27
memberhujik27-Feb-14 21:27 
QuestionSubItems Pin
jung-kreidler7-Sep-11 22:10
memberjung-kreidler7-Sep-11 22:10 
GeneralCombobox not created Pin
MichaelTR27-Jun-11 4:00
memberMichaelTR27-Jun-11 4:00 
GeneralRe: Combobox not created Pin
Blowfleecy7-Sep-11 15:56
memberBlowfleecy7-Sep-11 15:56 
GeneralMy vote of 4 Pin
jmandlia23-Feb-11 23:39
memberjmandlia23-Feb-11 23:39 
GeneralMy vote of 5 Pin
Tony's Toy16-Aug-10 21:23
memberTony's Toy16-Aug-10 21:23 
GeneralFree Memory Read in CPropertyGridInPlaceEdit::EndEdit Pin
Mark Potter11-May-10 5:23
memberMark Potter11-May-10 5:23 
GeneralRe: Free Memory Read in CPropertyGridInPlaceEdit::EndEdit Pin
Mark Potter11-May-10 5:34
memberMark Potter11-May-10 5:34 
GeneralParent window shouldn't be deactivated Pin
iceway18-Oct-09 20:54
membericeway18-Oct-09 20:54 
GeneralUnified Unicode/ANSI version Pin
Duncan Mackay11-Jun-09 19:22
memberDuncan Mackay11-Jun-09 19:22 
QuestionVB.NET or C# version? Pin
JeffThePeff16-Apr-09 4:33
memberJeffThePeff16-Apr-09 4:33 
QuestionHow to use without dialog box Pin
iznet25-Feb-09 12:25
memberiznet25-Feb-09 12:25 
AnswerRe: How to use without dialog box Pin
Nicolas Bonamy25-Feb-09 19:47
memberNicolas Bonamy25-Feb-09 19:47 
GeneralRe: How to use without dialog box Pin
iznet26-Feb-09 10:11
memberiznet26-Feb-09 10:11 
QuestionShow some special characters like [ \t ]? Pin
termal4-Dec-08 5:30
membertermal4-Dec-08 5:30 
QuestionGet pointer to combo item?? Pin
termal3-Dec-08 5:33
membertermal3-Dec-08 5:33 
GeneralIf more then one combo box selection problem! Pin
termal26-Nov-08 1:14
membertermal26-Nov-08 1:14 
GeneralRe: If more then one combo box selection problem! Pin
Nicolas Bonamy26-Nov-08 5:29
memberNicolas Bonamy26-Nov-08 5:29 
GeneralRe: If more then one combo box selection problem! Pin
termal26-Nov-08 7:42
membertermal26-Nov-08 7:42 
GeneralExpandfunction dont work! [modified] Pin
termal20-Nov-08 23:40
membertermal20-Nov-08 23:40 
GeneralRe: Expandfunction dont work! Pin
termal24-Nov-08 21:59
membertermal24-Nov-08 21:59 
GeneralSolved: Re: Expandfunction dont work! Pin
termal24-Nov-08 23:51
membertermal24-Nov-08 23:51 
GeneralRe: Expandfunction dont work! [modified] Pin
hujik27-Feb-14 21:23
memberhujik27-Feb-14 21:23 
GeneralItem Changed message not posted from EM_DROPDOWN Custom Items [modified] Pin
Mark Potter8-Jul-08 6:10
memberMark Potter8-Jul-08 6:10 
QuestionScrollbars in Combo Box? Pin
EqualsP17-Jun-08 7:07
memberEqualsP17-Jun-08 7:07 
AnswerRe: Scrollbars in Combo Box? Pin
Mark Potter7-Jul-08 5:51
memberMark Potter7-Jul-08 5:51 
GeneralRe: Scrollbars in Combo Box? Pin
Mark Potter8-Jul-08 5:28
memberMark Potter8-Jul-08 5:28 
GeneralRe: Scrollbars in Combo Box? Pin
jmandlia23-Feb-11 23:35
memberjmandlia23-Feb-11 23:35 
GeneralFont Selection Pin
Mark Potter31-May-08 10:35
memberMark Potter31-May-08 10:35 
GeneralAddItem( ...., after ) does not appear to work. Pin
Mark Potter29-May-08 10:38
memberMark Potter29-May-08 10:38 
GeneralRe: AddItem( ...., after ) does not appear to work. Pin
Member #44886807-May-09 1:14
memberMember #44886807-May-09 1:14 
GeneralRe: AddItem( ...., after ) does not appear to work. Pin
Mark Potter8-May-09 2:30
memberMark Potter8-May-09 2:30 
QuestionLicense? [modified] Pin
Mark Potter30-Apr-08 8:07
memberMark Potter30-Apr-08 8:07 
AnswerRe: License? Pin
Nicolas Bonamy2-May-08 3:30
memberNicolas Bonamy2-May-08 3:30 
GeneralRe: License? Pin
Mark Potter2-May-08 6:09
memberMark Potter2-May-08 6:09 
GeneralDLL Import Pin
E! Ray K23-Apr-08 8:36
memberE! Ray K23-Apr-08 8:36 
NewsUNICODE version uploaded Pin
Tomas125-Mar-08 10:35
memberTomas125-Mar-08 10:35 
GeneralRe: UNICODE version uploaded Pin
Damir Valiulin16-May-08 18:27
memberDamir Valiulin16-May-08 18:27 
GeneralRe: UNICODE version uploaded Pin
Tomas117-May-08 10:35
memberTomas117-May-08 10:35 
GeneralRe: UNICODE version uploaded Pin
Damir Valiulin18-May-08 5:08
memberDamir Valiulin18-May-08 5:08 
GeneralRe: UNICODE version uploaded - small bug Pin
Razmar18-Sep-08 13:57
memberRazmar18-Sep-08 13:57 
GeneralRe: UNICODE version uploaded - small bug Pin
Member #44886806-May-09 23:23
memberMember #44886806-May-09 23:23 
GeneralRe: UNICODE version uploaded [modified] Pin
Member #44886806-May-09 18:44
memberMember #44886806-May-09 18:44 
QuestionC# code for Customizable PropertyGrid :: Current Article Pin
Virat Kothari18-Nov-07 20:51
memberVirat Kothari18-Nov-07 20:51 
GeneralRe: C# code for Customizable PropertyGrid :: Current Article Pin
xb zhang17-Dec-07 21:13
memberxb zhang17-Dec-07 21:13 
AnswerRe: C# code for Customizable PropertyGrid :: Current Article Pin
Virat Kothari18-Dec-07 19:04
memberVirat Kothari18-Dec-07 19:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 25 May 2007
Article Copyright 2007 by Nicolas Bonamy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid