Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CListCtrl selection change (2)

Posted on 2000-05-06
2
Medium Priority
?
779 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 300 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

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

671 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