Solved

Setting Current Row in a CListCtrl

Posted on 2009-05-11
7
1,258 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
haveThree challenge 22 100
EvenOdd challenge 10 83
method argument as final 1 62
gdb doesn't stop on breakpoint 2 7
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

19 Experts available now in Live!

Get 1:1 Help Now