Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / MFC

Customized Report List Control With In Place Combo Box & Edit Control

Rate me:
Please Sign up or sign in to vote.
4.57/5 (27 votes)
29 Dec 2003CPOL4 min read 256.9K   9K   64   60
List control that supports in place combo box and edit control within the cells as well as read only columns

Image 1

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.

// Sets/Resets the column, which support the in place combo box 
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.

// Sets/Resets the column, which support the in place edit control 
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.

// Sets the valid characters for the edit ctrl 
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().

// Enables the vertical scroll if the bool passed is true 
// Disables the vertical scroll if the bool passed is false 
// The default value for the argument is true 
void EnableVScroll(bool bEnable = true); 

// Enables the horizontal scroll if the bool passed is true 
// Disables the horizontal scroll if the bool passed is false 
// The default value for the argument is 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.

// This message is posted to the parent 
// The message can be handled to make the necessary validations, if any 
#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.

// This message is posted to the parent
// The message should be handled to specify the items 
// to the added to the combo
#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");
    }
} 

License

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


Written By
Web Developer
Japan Japan
The Saint is a software professional having worked extensively on UI development on VC++ in the intial years.

He currently works as a System Analyst catering to project development & management. He loves to explore & improve his all round knowledge & technical skills.

He is addicted to playing computer games, loves to travel & listen to music.

Comments and Discussions

 
GeneralRe: ON_MESSAGE(WM_PASTE, OnPaste) Pin
Jason Callis27-Mar-06 22:46
Jason Callis27-Mar-06 22:46 
GeneralRe: ON_MESSAGE(WM_PASTE, OnPaste) Pin
Shlomy Reinstein19-Nov-08 23:05
Shlomy Reinstein19-Nov-08 23:05 
GeneralDropDown Style Fails ! Pin
Anonymous9-Sep-04 8:51
Anonymous9-Sep-04 8:51 
GeneralRe: DropDown Style Fails ! Pin
yumin10147-May-05 0:49
yumin10147-May-05 0:49 
Generalcontrol in subitem without selectioning Pin
quzi25-Dec-03 3:53
quzi25-Dec-03 3:53 
GeneralRe: control in subitem without selectioning Pin
The.Saint25-Dec-03 17:30
The.Saint25-Dec-03 17:30 
GeneralRe: control in subitem without selectioning Pin
quzi28-Dec-03 5:38
quzi28-Dec-03 5:38 
GeneralRe: control in subitem without selectioning Pin
The.Saint28-Dec-03 17:01
The.Saint28-Dec-03 17:01 
GeneralRe: control in subitem without selectioning Pin
quzi28-Dec-03 19:14
quzi28-Dec-03 19:14 
GeneralRe: control in subitem without selectioning Pin
quzi24-Mar-04 21:29
quzi24-Mar-04 21:29 

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

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