Navigating through CListCtrl's rows with the arrow and tab keys

Environment: Demo was created with Visual C++ 6.0 .

I needed to be able to edit some fields from a database, and wanted to have a label, the field name, followed by an edit box that the user could enter the field data in. The only problem was that there could be anywhere from 1 to 50 fields. So I used a CListCtrl with two columns to represent the field name and the field value. There are many articles here that show how to edit subitems, but I only needed to be able to edit one item on each row, so I just rearranged the columns to have the editable item be the second cloumn. The following code is all that is necessary to rearrange the columns.


int aiCol[2] = { 1, 0 };
m_List.SetColumnOrderArray (2, aiCol);
I also needed the user to be able to navigate through the fields with the tab and arrow keys. This was a pain because the CListCtrl creates its own edit box for editing items, and it was stealing all the key strokes that I wanted. I did not want to have to draw and manage my own edit box, so I just created my own CEdit based class and subclassed the edit box created by the CListCtrl. To subclass the edit control with your own edit control, m_Edit, all you need is:
Inside a message handler for the LVN_BEGINLABELEDIT message, you need to add the following code:

HWND hWnd = (HWND)SendMessage (LVM_GETEDITCONTROL);
ASSERT (hWnd != NULL);
if (m_Edit.m_hWnd != 0)
	m_Edit.DestroyWindow ();
VERIFY (m_Edit.SubclassWindow (hWnd));
Inside a message handler for the LVN_ENDLABELEDIT message, you need to add the following code:

if (m_Edit.m_hWnd)
	VERIFY (m_Edit.UnsubclassWindow () != NULL);
You can then add a message handler in your m_Edit class to catch the KeyDown event and then begin editing the next item.

The example program is simply a dialog box with an CListCtrl drawn on it. Ten fields are added in the OnInitDialog function to illustrate this functionality.

Download demo - 19 KB

Date Last Updated: April 24, 1999



Comments

  • Different Way

    Posted by Legacy on 06/27/2001 12:00am

    Originally posted by: KalliMan

    Hi, 
    
    Do You Know A Different Way To Do That, I Mean, Without
    "SetColumnOrderArray()"?

    Reply
  • Multiple Column?

    Posted by Legacy on 05/21/1999 12:00am

    Originally posted by: Christian

    Do you know how navigate on more that 1 column?
    (right<->left)
    When i press TAB i want go edit the right item and inverse with Shift-TAB.
    thanks

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

Top White Papers and Webcasts

  • On-demand Event You can scale your mobile apps' data throughput and storage elastically with a fully managed, planet-scale NoSQL database service built for fast and predictable performance, high availability, elastic scaling, global distribution, and ease of development. This 40-minute on-demand webinar will demonstrate the scalability of DocumentDB and explore how the MongoDB API for DocumentDB, along with Xamarin, can simplify mobile application development for your organization. About Azure …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date