viksoe.dk

PropertyGrid control

PropertyGrid control


This article was submitted .


This is the 3rd control built using a set of reusable in-place editor windows and classes. This time it is a grid control.

A grid control is similar to a normal list control, except that each sub-item is editable. Sub-items can contain text values (for editing), dropdown lists, filename browsers or pretty much any type imaginable.

Just as my other property controls (the PropertyList and PropertyTree controls), it's using the same selection of in-place editors, ranging from dropdown lists to filename browsers. They are all implemented as standard windows, which link to the parent control through a generic interface and some shared window messages. Because of this design, you may extend the control with your own property types.
The control is a subclassed ListView control.

The nice thing about this grid control is that it uses the same navigation as a normal spreadsheet grid, and that it has many familiar keyboard-shortcuts (F2 key and space works as expected). It doesn't do sofisticated row/column selection or copy/paste functionality, but for a fast type-in grid control it's very nice.

How to use it

Place a ListView control on a dialog.
Add a member variable to your dialog class...
CPropertyGridCtrl m_Grid
In the OnInitDialog() event handler, add the following line:
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPA...
{
  ...
  m_Grid.SubclassWindow(GetDlgItem(IDC_LIST1));
  ...
}
Then add the following reflection macro to your main message map:
BEGIN_MSG_MAP(CMainDlg)
  ...
  REFLECT_NOTIFICATIONS()
END_MSG_MAP()

Here are some of the control methods available:
InsertItem
SetSubItem
GetItemText
GetItemValue
SetItemValue
GetItemEnabled
GetItemEnabled
GetItemData
SetItemData
and several of the original CListViewCtrl methods still work as well.

Just like the ListView control, you need to add column headers first.

  m_Grid.InsertColumn(0, "Name", LVCFMT_LEFT, 200, 0);
Once you have added your column headers, you cannot add or delete them again.

Adding a new property requires using a "creation helper" method...

  m_Grid.InsertItem(0, PropCreateSimple("", "Raymond"), 0);
  m_Grid.SetSubItem(0, 1, PropCreateSimple("", false));
  m_Grid.SetSubItem(0, 2, PropCreateCheckButton("", true));
Note how the Name argument (1st argument) is empty for the creation helper routines since properties in this control do not have an attached label.
You may also associate some item-data to each property using the SetItemData() method, so it can later be identified.
The PropertyList control sample explains how to use some of the other editors.

Several notifications are sent back to the owner window. They contain information about the changing property using WM_NOTIFY notification structure: NMPROPERTYITEM. The list of notifications currently include:

PIN_SELCHANGED
PIN_ITEMCHANGING
PIN_ITEMCHANGED
PIN_BROWSE
PIN_CLICK
In addition, all of the original ListView notifications are also available. Use the GetSelectedIndex() and GetSelectedColumn() methods to located the selected property.

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.1 Library

See Also

My Property List control
My Property Tree control
My ChoiceBar control for adding your own pop-ups

Download Files

DownloadSource Code and sample (55 Kb)

To the top