Introduction
List control especially with the “report” style is one of the most commonly used controls in any UI.
At times as a developer, we would like to be able to
- Select from a list of items for the value of a cell
- Key in the value for any of the cells
- Entire read only column
Developing a list control with these features takes substantial amount of time. I have just tried to create reusable classes that would provide the developer to accommodate in place drop down combo box and edit control.
The columns of the list control can be assigned the above mentioned properties through the public interfaces. This sample is also an example of a control embedded within another control by making the embedded control an attribute of the parent control. By default all the cells support in place edit control.
The code for this article was written on Windows 2000 Professional with Microsoft Visual C++ 6. I have tested the and used the same in my work.
Features
The developer who needs to use a list control as described above just has to include the following files in the project
- ComboListCtrl.cpp
- ComboListCtrl.h
- InPlaceEdit.cpp
- InPlaceEdit.h
- InPlaceCombo.cpp
- InPlaceCombo.h
The developer will need to create a list control resource and associate a control member variable with the same. Then type “CListCtrl
” will have to be modified to “CComboListCtrl
”.
The columns that need to support in place combo box, edit control and read only property can be specified using the public interfaces available.
The list of items to be shown in the drop down of the combo box can be specified by the parent class. This allows different columns to have different set of items.
In case of the edit control, the valid characters can be specified. If nothing is specified then all characters are considered valid. In addition to the internal validations for characters being keyed in and pasted, the parent control is given a notification for further validations. This allows the parent class to handle any validation specific to the format.
The read only columns as the name suggests is beyond the end user manipulation.
The developer can also enable or disable the horizontal and vertical scroll bars for the in place combo box.
Public Methods
The columns which need to support the in place combo box can be set by passing the column index to SetComboColumns
. By default this function will enable the support of combo box in the cells. The combo box support can be reset by passing the second default argument as false.
void SetComboColumns(int iColumnIndex, bool bSet = true);
The columns which need to act as read only can be set by passing the column index to SetReadOnlyColumns
. By default this function will enable the read only property in the cells. The combo box support can be reset by passing the second default argument as false.
void SetReadOnlyColumns(int iColumnIndex, bool bSet = true);
The valid characters for the edit control can be set by SetValidEditCtrlCharacters
. The in place edit control will allow only these characters to be keyed in or pasted to the control. On pasting either all invalid characters or a combination of valid and invalid characters, the paste operation will not succeed.
void SetValidEditCtrlCharacters(CString& rstrValidCharacters);
The combo box usually has the ability to support vertical and horizontal scroll bars. But in case of an embedded in place control the need for the same would depend on the usage. The scroll bars can be enabled or disabled by the following interfaces which function much similar to the EnableWindow()
.
void EnableVScroll(bool bEnable = true);
void EnableHScroll(bool bEnable = true);
User Defined Messages
The text entered in the edit control may need to be validated for its format as per the usage. For this the in place control will have to send a message to the parent of the list control indicating that end of edit operation. The user defined message WM_VALIDATE
is used sent to the parent to indicate the end of editing. The parent class may handle this message to make the necessary validations for the format.
#define WM_VALIDATE WM_USER + 0x7FFD
The items in the combo box will have to be externally specified by the parent of the list control. The WM_SET_ITEMS
message is posted using ::SendMessage()
to the parent. The parent can handle this message and fill in the list of items to be shown in the combo box. The items can be different for each column. This can be achieved as shown in the example below.
#define WM_SET_ITEMS WM_USER + 0x7FFC
Example
LRESULT CMyDialog::PopulateComboList(WPARAM wParam, LPARAM lParam)
{
CStringList* pComboList = reinterpret_cast<CSTRINGLIST*> (lParam);
pComboList->RemoveAll();
if (iColumnIndex == 1)
{ pComboList->AddTail("Name1");
pComboList->AddTail("Name2");pComboList->AddTail("Name3");
}
else if (iColumnIndex == 2)
{pComboList->AddTail("Age1");
pComboList->AddTail("Age2");pComboList->AddTail("Age3");
}
}