Solved

Setting Current Row in a CListCtrl

Posted on 2009-05-11
7
1,282 Views
Last Modified: 2013-11-20
I have a CListCtrl that uses a virtual list with user drawn data allowing for mulitple selections.
When the user clicks on a header, the logic changes the sort order and re-sorts the data.
What is working great is the logic that first records which items are currently selected, resorts the list, and reselects the selected items even though they have moved positions.
But what else is happening to me is the "current" item does not change.

As an example, say I have a List where Item #2 is selected.  If I hit the down arrow, the list changes the selected item to #3.  If I click a header and resort the data, what was item #3 is not item #7, and item #7 is selected (by virtue of the item highlighted in blue).  But item #3 still has a dash rectangle around it, and if I hit the down arrow, item #4 becomes selected rather than #8.

I am NOT using Hot cursors.  Based on the documentation, I would have thought CListCtrl::SetSelectionMark would allow me to update which item that dash rectangle would apper.  But when I tried hard coding the selection mark to say item #2, the dash rectangle didn't move.

So what do I need to call to move that dash rectangle?  
0
Comment
Question by:HooKooDooKu
  • 3
  • 2
  • 2
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24361526
Actually it is behaving correctly, just because the contents of the selected row have been changed doesn't mean it 'searches' for where the previous information was.

Deselect the row selected, sort, select the row you now want selected.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24362686
>>>> Actually it is behaving correctly
Don't think so. The hi-lited and selected row also should be the focused one.

You might consider on handling the LVN_ITEMCHANGED notification message in your dialog or view class. Then for any change of selection you could update the state of the selected item so that it is focused as well.

void CTestDlgDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
   LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
   if (myList1.GetItemState(pNMLV->iItem, LVIS_FOCUSED) == 0) 
   {
       myList1.SetItemState(pNMLV->iItem, LVIS_FOCUSED,
                                          LVIS_FOCUSED);
   }
 
    *pResult = 0;
}

Open in new window

0
 
LVL 16

Author Closing Comment

by:HooKooDooKu
ID: 31580396
BINGO!
Didn't have to handle any notification messages.  Just had to update the code where I was already using SetItemState() with LVIS_SELECTED to also specify LVIS_FOCUSED for the 1st item that gets selected.

Thanks.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24365197
>>>> Just had to update the code where I was already using SetItemState() with LVIS_SELECTED

Good. Assume you now have

myList1.SetItemState(item, LVIS_FOCUSED | LVIS_SELECTED
                                   LVIS_FOCUSED | LVIS_SELECTED);

Or you may add the LVIS_DROPHILITED both for settings and mask argument.

But you have to be cautious when actively calling SetItemState() so to not get a deadlock when handling any notifications. Unfortunately the SetItemxxx calls
cannot be made to not invoking new notifications. As long as you didn't handle the notifications you are safe.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24365326
>>>> Just had to update the code where I was already using SetItemState() with LVIS_SELECTED



I assume that came from a comment to the expert given the points.
precisely - what I told you to do.
0
 
LVL 16

Author Comment

by:HooKooDooKu
ID: 24365940
@ itsmeandnobodyelse
Fortunately I am either not responding to the generated notifications, or for the ones I am responding to, the response is to update another window with additional data that matches the FOCUSED item.  So it's working great, but thanks for the warning.

@AndyAinscow
My problem was NOT with what items were selected.  I had long ago learned how to use SetItemState() and LVIS_SELECTED to select and unselect items.

My problem was that I only recently learned I had a problem with the FOCUS rectangle.  My problem was that I didn't know how to update the FOCUS rectangle (I just couldn't think to call it that) and didn't even think to relook at SetItemState().

itsmeandnobodyelse was given the points for typing two word: "SetItemState" and "LVIS_FOCUSED".  That was what I was missing.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24366043
Aha, now I get it.  Thanks for clarifying.  (When I select an item I set the focus at the same time  LVIS_FOCUSED | LVIS_SELECTED, It didn't occur to me that you wouldn't realise that was required. )
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 82
noX challenge 17 119
Why isn't object file created? 6 59
How to properly read C++ console input into several variables with scanf 2 12
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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.

821 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