Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


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 45

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 45

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.
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

LVL 45

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 45

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

647 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