Question

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?

Was it helpful?

Solution

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`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top