简体   繁体   中英

CListCtrl selection

I'm trying to do something that I think is simple but I can't seem to make it work!

I have a CListCtrl and I want to select the last element in the list if the user clicks in the view empty space. I can do that just by calling Select(lastElementInList) , but the element that was previously selected and that is now unselected still has a "bounding rectangle" around it.

The code that Implements this is as follows:

    int nSel = GetNextItem(-1, LVNI_SELECTED);
    if (nSel != -1)
        SetItemState(nSel, 0, LVIS_SELECTED);

    Select(lastElementInList);

Any hints? What am I missing?

The "bounding rectangle" you see indicates that the element currently is "focused" , ie. in a state where a user interaction, such as pressing the down and up arrows, would start off from this point.


Change focused element

To move focus to your newly selected element you'll have to use SetItemState together with LVIS_FOCUSED , as in the below example:

if (nSel != -1)
    SetItemState (nSel, ~LVIS_FOCUSED, LVIS_FOCUSED);          // (1)

SetItemState (lastElementInList, LVIS_FOCUSED, LVIS_FOCUSED);  // (2)

// (1) -> Remove focus from `nSel`
// (2) -> Add focus to `lastElementInList`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM