Why keyboard navigation starts working only from second keypress in my list view control?
By Yaroslav Goncharov, July 18, 2002.
Question
I have created a single-selection list view control inside the dialog, set the focus to this control and selected the second
item using the following code:
ListView_SetItemState(hWndListCtrl, 1, LVIS_SELECTED, LVIS_SELECTED);
However, the list view control does not respond to any arrow keys except the <DOWN> arrow. But
the <DOWN> arrow moves the selection from the second item to the first one! What am I doing wrong?
Answer
There are 2 item states of list view control in related to this problem: LVIS_SELECTED and LVIS_FOCUSED. On Smartphone 2002 it is
important to understand the difference between these states since Smartphone 2002 user interface cannot breathe
whithout keyboard navigation.
LVIS_SELECTED means that the item is selected. The appearance of a selected item depends on whether it has the focus and also
on the system colors used for selection.
LVIS_FOCUSED means that the item has the focus, so it is surrounded by a standard focus rectangle.
Although more than one item may be selected, only one item can have the focus.
LVIS_FOCUSED state is used for keyboard navigation in the list view control. In a single-selection control
the selection follows the keyboard focus. In your situation the second item has the selection, but does not have the focus.
So, you need to set the focus to the desired item as well as selection.
ListView_SetItemState(hWndListCtrl, 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
If you use WTL in your project you can use CListCtrlView::SelectItem to achieve the same effect.
// this fragment is from WTL Version 3.1 sources
// single-selection only
BOOL SelectItem(int nIndex)
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((GetStyle() & LVS_SINGLESEL) != 0);
BOOL bRet = SetItemState(nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
if(bRet)
bRet = EnsureVisible(nIndex, FALSE);
return bRet;
}
Discuss
Discuss this QA.
Here you can write your comments and read comments of other developers.
|