CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Change Selection in CListCtrl when Arrow Keys are pressed

    By default, don't the up/down arrows change the selection?

    I mean isn't this the behavior if you just put a listview control on the page and add some items to it? After the selecting an item, the up/down arrows should work without any additional code.

  3. #3
    Join Date
    Jun 2009
    Posts
    14

    Re: Change Selection in CListCtrl when Arrow Keys are pressed

    I tried it and you're right. That explains a lot :-S Thank you. I would've wasted a couple more hours on this...
    This way though the selection doesn't "jump" to the first item when pressing the down arrow and the last item is selected. Maybe i can at least handle that case in OnLvnKey....

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Change Selection in CListCtrl when Arrow Keys are pressed

    Quote Originally Posted by maglite View Post
    This way though the selection doesn't "jump" to the first item when pressing the down arrow and the last item is selected. Maybe i can at least handle that case in OnLvnKey....
    You could, but this isn't standard Windows behavior. You'll notice for example, that a list of files in Windows Explorer doesn't wrap when you down arrow and reach the bottom.

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