Solved

Difference between CListCtrl and CListBox Item changed message

Posted on 2008-10-13
7
1,686 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
Technology Partners: 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 70
Copy output image from TWindowsMediaPlayer 6 55
designing in object programming 12 109
find Speed using GPS latitude & longitude 4 95
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

730 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