Re: Moving in a Listview
"David Lowndes" <DavidL@example.invalid> wrote in message
news:v36eh3hi1cu4imb6i50770n3u3a7jtm68q@4ax.com...
I am using a ListView (CListCtrl) to show numerical data in a grid and a
second view where data is shown graphically. When the user is moving the
cursor in the graphics view I would like to synchronize the grid view so
the
current line in the listview should be changed accordingly. How can be set
the current line (which is shown by inverse representation) in the
listview?
Use CListCtrl::SetItemState with LVIS_SELECTED.
It's also helpful to set the item to be focused as well as selected so that
if the user uses the arrow keys or PgUp/PgDn, the selection is moved
relative to selected item. It drives me bonkers that standard list controls
can have different focus and selected items... I use the keyboard to move
the selection up one item but instead it jumps the list to some invisible
item (the one with the focus) and moves the selection from there instead.
Truly irrritating.
Here's code to set both focus and selection:
BOOL CMyListCtrl::SetFocusToItem (int i)
{
int n = GetItemCount();
if (n < 0) // no items in list
return TRUE;
// Remove selections
POSITION pos = GetFirstSelectedItemPosition();
while (pos)
{
int iItem = GetNextSelectedItem(pos);
SetItemState (iItem, 0, LVIS_SELECTED);
}
// Remove existing focus;
int iFocus = GetItemWithFocus ();
if (iFocus >= 0)
SetItemState (iFocus, 0, LVIS_FOCUSED);
// Set focus/selection
if (i < n) // valid item
SetItemState (i, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED |
LVIS_SELECTED);
return TRUE;
}
-- David