Solved

CListCtrl selection change (2)

Posted on 2000-05-06
2
770 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now