Solved

Setting Current Row in a CListCtrl

Posted on 2009-05-11
7
1,265 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now