Solved

CListCtrl selection change (2)

Posted on 2000-05-06
2
778 Views
Last Modified: 2013-11-20
One of the most common events to be handled for listboxes is the fact that the item selection has changed. However I did not find any direct way of handling such event. What I am doing is Handling the LVN_ITEMCHANGED msg and using nasty condition like this:

void Foo::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView =(NM_LISTVIEW*)pNMHDR;

if (!(pNMListView->uOldState & LVIS_SELECTED) && 
(pNMListView->uNewState & LVIS_SELECTED)
|| (pNMListView->uOldState & LVIS_SELECTED) && 
!(pNMListView->uNewState & LVIS_SELECTED))
{
//do sth on selection change
}
}
Apart from the ugliness of this solution, the code under the condition is often called twice per one selection change (for item being deselected and for item that gets selected). I would like to have sth. that would work like LBN_SELCHANGE msg. for simple listbox.
Is there any nice solution?

0
Comment
Question by:ewajoz
[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
2 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 2784576
Here's the useful fn I use in a variety of related cases:
-=-=-=-=-=-=-=-=-=-
int GetSelItemIdx( CListCtrl& ctlList )
{
  int  nItmIdx= -1;
  if ( ctlList && ::IsWindow(ctlList.m_hWnd) && ctlList.GetSelectedCount() > 0 ) {
    POSITION pos= ctlList.GetFirstSelectedItemPosition();
    if (pos != NULL) {
        nItmIdx= ctlList.GetNextSelectedItem(pos);
    }
  }
  return( nItmIdx );
}
-=-=-=-=-=-=-=-=-=-

Your handler can call this to get the current selection and (possibly check to see if it is the same as the previous one before taking action).

As to the double notifications:  Just code to check the uNewState:

if ( (pNMListView->uNewState & LVIS_SELECTED) ) {
  /// user just selected a new (previously unselected) item
}
 
Another hint: It simplifies your task to check the Single Selection option in the List Control Properties in the dlg box editor. (I have rarely found the need for multiple selections, and when I need that sort of functionality I use the ListBox control that has checkboxes in it).


0
 

Author Comment

by:ewajoz
ID: 2784604
Thanks, DanRollins
With respect to the first part of your comment: your method requires storing recently selected item or even items, if the list is multiple selection (mine is), which somehow I do not fancy too much.
What refers to the second part: when only checking against the LVIS_SELECTED you lose the situation, when the user deselects an item (by clicking on the selected item, or on the area beyond items) That is why my condition is so huge.
Using checkboxes combined with single selection is certainly a good idea, which I will probably apply.
0

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

636 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