Customized Report List Control with In-Place Combo Box '& Edit Control

Environment: VC6, Windows 2000

Introduction

A 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
  • Create an entire read-only column

Developing a list control with these features takes a ssubstantial amount of time. I have just tried to create reusable classes that would allow the developer to accommodate an 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 the 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, the CListCtrl type will have to be modified to CComboListCtrl.

The columns that are needed to support an in-place combo box, edit control, and read-only property can be specified by 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 sets of items.

In the case of the edit control, the valid characters can be specified. If nothing is specified, 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's manipulation.

The developer can also enable or disable the horizontal and vertical scroll bars for the in-place combo box.

Public Methods

The columns that 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 a 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 supports the in-place combo box
void SetComboColumns(int iColumnIndex, bool bSet = true);

The columns that 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 supports 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 control
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 the same as 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 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 by 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 be 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");
  }
}

Downloads

Download demo - 10 Kb
Download source - 26 Kb


Comments

  • Needing to set focus on a control if empty

    Posted by nebnetjeruiti on 04/14/2006 11:28pm

    Thanks for the great code! Its working out really well! I am very new to C++ and MFC, and I learn well from example code, so your patience and help through my stupidity is really appreciated:-D! I have managed to use the code to make a column read-only if the previous field doesn't have a value....I've also managed to populate a combobox from a database based on the previous field's value....however, I have been unable to find a way to set the focus back on a specific field if its string value is empty. For instance, I have an edit field....If it doesn't contain any text, I want to messagebox the user that the field is required, and when the user clicks "OK" then to set the focus back to that control. I have edited OnEndLabelEditVariableCriteria() to include the following: case 3: if(!strUpdatedTxt.IsEmpty()) { MessageBox(strUpdatedTxt); } What can I put in the "else" part so that when the user presses ENTER, which then sends a message to the PreTranslateMessage() function in InPlaceEdit.cpp, to tell the program to set focus back to the "virtual" CEdit if it is empty?

    Reply
  • Default Values for Combo Box

    Posted by tkedar on 10/26/2005 06:56pm

    I have a fixed set of rows in the List Control. I need to be able to fill the ComoBox in Column 1 with some default values. How can I do that? I was able to populate using LButtonDown provided but want to display a default selected item when the dialog is loaded for the first time.

    Reply
  • Static_cast error in V C++ 7.1

    Posted by AsaF on 06/14/2005 03:55am

    Hi, very nice and easy to use in V C++ 6.0, but I get an error when converting the project to 7.1: d:\Projects\FontDlg71\FontList\FontListDlg.cpp(44): error C2440: 'static_cast' : cannot convert from 'void (__thiscall CFontListDlg::* )(void)' to 'void (__thiscall CCmdTarget::* )(NMHDR *,LRESULT *)' Line 44: ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnLstLClick) This worked fine in 6.0 and I can't figure out what must be changed. Anyone who has a clue?

    • Static_cast error

      Posted by fellun on 08/28/2005 08:42am

      Hello, Is there someone helpful person out there who can help me to solve the static cast error? Please... /Felix

      Reply
    Reply
  • Very Nice

    Posted by wwwalrus on 03/03/2005 08:49am

    You've saved me much time

    Reply
  • Source code

    Posted by rvtchandu on 02/21/2005 03:42am

    Where is code?

    • hi

      Posted by joshwakang on 05/11/2005 02:22am

      hi

      Reply
    Reply
  • Combo does not open when list control don't have LVS_EX_FULLROWSELECT

    Posted by Legacy on 02/19/2004 12:00am

    Originally posted by: Haim

    Hi,
    The HitTest fails to find the clicked item if the extended style LVS_EX_FULLROWSELECT is not applied to the list control.
    Is there a reason why?

    • A limitation

      Posted by The Saint on 08/02/2004 12:48am

      Hi, Currenlty it is mandatory to use the LVS_EX_FULLROWSELECT extended style.

      Reply
    Reply
  • how to read from the combo box?

    Posted by Legacy on 02/17/2004 12:00am

    Originally posted by: nagraj

    you have done a really great job..can you tell me how to read from the combo box

    • Reading from the combo box

      Posted by The Saint on 08/02/2004 12:43am

      Hi, Sorry for the late reply. Was away for quite a long time. You can read the data from the combo box by handling the user defined WM_VALIDATE message by associating the same with OnEndLabelEdit method. You could also simply use the GetItemText method.

      Reply
    • you have done a really great job..can you tell me how to read from the combo box

      Posted by wonbokyu on 07/18/2004 10:25pm

      you have done a really great job..can you tell me how to read from the combo box

      Reply
    Reply
  • Great

    Posted by Legacy on 01/08/2004 12:00am

    Originally posted by: hari

    What I'd been looking for...

    Reply
  • Good work...

    Posted by Legacy on 01/07/2004 12:00am

    Originally posted by: Hans Wedemeyer

    thanks.
    hansw

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds