Solved

CListCtrl selection change (2)

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 84
not able to insert into temp table 68 162
countEvens challenge 2 99
My project did see openJDK that I installed. What could be the problem 7 141
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

776 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