Difference between CListCtrl and CListBox Item changed message

Posted on 2008-10-13
Medium Priority
Last Modified: 2013-11-20
I am really having trouble understanding why the List changed functions of listbox and listctrl behave differently.
In my application i have 2 controls, a listbox and a list control.
for OnLbnSelChange function of the list box, even if i click anywhere on the listbox, the GetCurSel() function always return the index of selected item, i.e. it keeps the item selected even if click on anywhere else on the list box.

For listctrl, if an item is selected and i click anywhere else on the list ctrl, the position of GetFirstSelectedItemPosition() is always returned null and the item is not highlighted anymore.

how can i have the same behaviour same as listbox with list control??
Question by:LearningCpp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 44

Expert Comment

ID: 22708993
>>how can i have the same behaviour same as listbox with list control??

Well a list box and a list control are VERY different controls.  A list control for example supports an ICON view totally unlike a list box which is just single rows.

>>For listctrl, if an item is selected and i click anywhere else on the list ctrl, the position of GetFirstSelectedItemPosition() is always returned null and the item is not highlighted anymore.

It sounds like you want to have the single selection option (see resource view / properties) turned off - the default behaviour is single selection.
LVL 19

Expert Comment

ID: 22709054
In the resource editor, you must set the property "Show Selection Always" to true
LVL 44

Expert Comment

ID: 22709228
>>and i click anywhere else on the list ctrl
The 'Show Selection Always' is only required if you click on another control such as the list box but still want the selection on the list control to be displayed. It has nothing to do with selecting multiple items on a list control.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 44

Accepted Solution

AndyAinscow earned 1500 total points
ID: 22709255
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
   TRACE0("No items were selected!\n");
   while (pos)
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here

Author Comment

ID: 22715682
I have Always Show Selection = true
as well as
Single Selection = true in my resource editor.

I have a function on OnLvnItemchangedListCtrl.
The req is to enable a button if any item is selected in the listcontrol.
so i have
if(pos == NULL)
CButton* pBtn;
pBtn = (CButton*) GetDlgItem(IDC_BTN_EDIT);
GotoDlgCtrl(pBtn); //to set focus on the edit button, so that after i select an item and press "SPACE on keyboard" the edit button will be invoked without user actually using mouse to click the button.(setfocus does not work here)

LVL 44

Expert Comment

ID: 22718498
Please post the complete code for the OnLvnItemchangedListCtrl. function.  (The snippets you posted should be functional).

Have you checked it is actually being called with a breakpoint for instance?

Author Closing Comment

ID: 31505721
I removed the while loop though, but when user clicks anywhere else then the focus is lost from listcontrol, strange, but will work for now

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question