Hi.
I have a CListCtrl in a modal Dialog. The control has the following styles:
- Report
- Single Selection
- No Scroll
- No Sort Header
- Always Show Selection
- Grid Lines
- Full Row Selection
In OnInitDialog I select the first line (Item) and save the selection in the variable m_nItem. So far so good. Selecting items via mouse click and saving the selection also works fine:
Now I want to programatically change the selection when either the Up or Down Key is pressed and go back to the first item when the list's end is reached (and also the other way round).Code:void CMyDlg::OnNMClickMyList(NMHDR *pNMHDR, LRESULT *pResult) { LPNMITEMACTIVATE temp = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR); if(temp->iItem == -1) // redundant as in my case only selectable items are visible return; m_nItem = temp->iItem; *pResult = 0; }
So this is my code for handling the LVN_KEYDOWN message:
I debugged the code and m_nItem is actually calculated correctly but the wrong item gets selected nevertheless. It seems like - instead of going up/down one item - steps of two items are taken.Code:void CMyDlg::OnLvnKeydownMyList(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR); switch(pLVKeyDow->wVKey) { case VK_DOWN: m_nItem = (m_nItem+1)%m_nItemCount; m_ctrlList.SetItemState(m_nItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); break; case VK_UP: m_nItem = (m_nItem-1+m_nItemCount)%m_nItemCount; m_ctrlList.SetItemState(m_nItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); break; } *pResult = 0; }
I've tried to leave out the LVIS_FOCUS flags and the control showed a different behaviour. Not a more satisfying one though. Still wrong selections from time to time.
Furthermore i tried various combinations of RedrawItems, Invalidate and UpdateWindow after SetItemState, which all seemed to have no effect whatsoever.
I'm pretty clueless. Please Help!
Thanks in advance,
Markus




Reply With Quote