Click here to Skip to main content
12,830,358 members (43,845 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 8 Jan 2004

How to easily navigate and edit a list view control

, 8 Jan 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
How to easily navigate and edit a list view control


We have a list view control; the task is to write simple code to help in navigating through the controls like how one navigates in an excel sheet, also we need edit the individual cells of the list view control again just like how we edit in an excel sheet. When I started to look for solutions over the net I found out the code to do such a thing was quite complicated, though it ultimately achieved what I set out for. I started to write my own code and my main objective was to keep it as simple as possible while achieving the expected results.

How does the control work?

First run the application, a list view control (3 rows by 9 columns) will appear in a dialog application. Press tab and a combo box appears in the first column, first row of the list View control. Pressing tab again will make the combo box to disappear and an edit box to appear in the second column, and so on. For navigating backwards you need to press shift+ tab. If you click anywhere except in the header and the last column (4th) in the list control a corresponding control will appear. Scrolling the mouse will make all controls disappear; using the scroll bars will also have a similar effect. As soon as the control is removed from a cell its contents are copied into the cell. I think the very basic necessities of a navigatable-editable list view control are satisfied.

How simple is the code?

Like many times before I will explain what am doing here. After you shoot up a dialog application and put a list view control in it. Derive a class from CListCtrl I even derived classes from CEdit and CComboBox just in case I need more control on the controls however I do not make any use of it. I define the following global variables

CMyEdit *m_pedit = NULL;
CMyCombo *m_pcombo = NULL;
int row =0;
int col =-1;

The OnInitDialog() function contains some standard code to get the List view control up and running. I declare the following public functions in the CMyListCtrl the class derived from CListCtrl

LRESULT OnCombo();
void OnSetText();

Use PreTranslateMessage(...) to accommodate for TAB and SHIFT+TAB keys. After the row column management is done I call these three functions

  • VerifyScrollPos();
  • vResetTopPosition();
  • vResetBottomPosition();.

The first one sets the scroll bar position to get the control into view if required. I have presented various flavours of this feature. The code is there and it commented out. The second adjusts the top position of the control, if it by chance goes above the header control. The third does the same to manage the position of the control if it goes below the Listview control. Remember that I do a SendMessage and not a PostMessage, because I need to wait till the scrolling is over. The code then proceeds to do switch on the col variable.

I have pre-assigned the first column for combo boxes the second and third for edit boxes. So here is where I call OnEdit() or the OnCombo(). The OnCombo() function checks to see if the m_pcombo is NULL . If it is NULL then no combo box is created it proceed to create one whose parent is the Listview control. and places it at the current row, col coordinates. If Edit box is already created it just moves it to its current row, col coordinates. The OnSetText() function just takes the current control's text and writes it on the current cell.

Then I handle the NM_CLICK message handler of the list view control to make a control disappear from its previous position and appear in the clicked position, this is done in OnClick(...). The LVN_COLUMNCLICK message is handled to make all the controls disappear. OnNotify(..) function prompts me when ever the user is trying to resize a column of the List view control, I grab the opportunity and hide all the controls so that the next time the can appear with new sizes. I have tested to my heart’s content and found that the behaviour is as close to that of an excel sheet. There might be still some bugs please feel free to fix them and notify me afterwards.


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


About the Author

Dr. Sai
Web Developer
India India
I have a PhD in pure mathematics from HCU. I have been programming in Microsoft technologies for more than a decade. My interests are C#, WPF Silverlight, MFC, COM.

You may also be interested in...


Comments and Discussions

Generaloh Pin
kedypen26-Dec-05 4:40
memberkedypen26-Dec-05 4:40 
Generalnice shoot Pin
sreejith ss nair8-May-05 18:58
membersreejith ss nair8-May-05 18:58 
Generaljust look at yr articles in, this time u show yr stuff in codeproject Pin
Anonymous9-Jan-04 2:46
sussAnonymous9-Jan-04 2:46 
GeneralRe: just look at yr articles in, this time u show yr stuff in codeproject Pin
Anonymous9-Jan-04 7:07
sussAnonymous9-Jan-04 7:07 
GeneralRe: just look at yr articles in, this time u show yr stuff in codeproject Pin
armentage9-Jan-04 7:09
memberarmentage9-Jan-04 7:09 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 9 Jan 2004
Article Copyright 2004 by Dr. Sai
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid