• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1427
  • Last Modified:

Setting Current Row in a CListCtrl

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
HooKooDooKu
Asked:
HooKooDooKu
  • 3
  • 2
  • 2
1 Solution
 
AndyAinscowCommented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
HooKooDooKuAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
itsmeandnobodyelseCommented:
>>>> 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
 
AndyAinscowCommented:
>>>> 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
 
HooKooDooKuAuthor Commented:
@ 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
 
AndyAinscowCommented:
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

Industry Leaders: 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!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now