Solved

CListCtrl selection change (2)

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
dog bark java program 15 111
withoutTen challenge 14 132
Problem to Office 1 15
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

830 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