Solved

Difference between CListCtrl and CListBox Item changed message

Posted on 2008-10-13
7
1,710 Views
Last Modified: 2013-11-20
Hi,
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??
0
Comment
Question by:LearningCpp
[X]
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
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 
LVL 19

Expert Comment

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

Expert Comment

by:AndyAinscow
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 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");
else
{
   while (pos)
   {
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here
   }
}
0
 

Author Comment

by:LearningCpp
ID: 22715682
Andy,
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)
disableButton()
else
Enablebutton()
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)


0
 
LVL 44

Expert Comment

by:AndyAinscow
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?
0
 

Author Closing Comment

by:LearningCpp
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

717 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