CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jun 2009
    Posts
    14

    Change Selection in CListCtrl when Arrow Keys are pressed

    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:

    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;
    }
    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).

    So this is my code for handling the LVN_KEYDOWN message:
    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)&#37;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 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.

    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
    Last edited by maglite; July 22nd, 2009 at 05:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured