Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CListCtrl arrow key navigation

CListCtrl arrow key navigation

Scheduled Pinned Locked Moved C / C++ / MFC
mcphelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dennisV
    wrote on last edited by
    #1

    Hello, Please help me with CListCtrl arrow key navigation - I have my own listctrl class, but when I use the arrow keys to scroll up and down though it, the current item is not identified correctly (I use UINT whichItem=GetNextItem(-1,LVNI_SELECTED)). It is updated one step behind, i.e. I'm on item 0, I press the down arrow, the whichItem is still 0, I press the down arrow again and then it becomes 1. Maybe I need to implement a keyboard handler in my derived class or something? :confused: Thanks in advance, Dennis MCP, MCSD

    S 1 Reply Last reply
    0
    • D dennisV

      Hello, Please help me with CListCtrl arrow key navigation - I have my own listctrl class, but when I use the arrow keys to scroll up and down though it, the current item is not identified correctly (I use UINT whichItem=GetNextItem(-1,LVNI_SELECTED)). It is updated one step behind, i.e. I'm on item 0, I press the down arrow, the whichItem is still 0, I press the down arrow again and then it becomes 1. Maybe I need to implement a keyboard handler in my derived class or something? :confused: Thanks in advance, Dennis MCP, MCSD

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #2

      Where are you trying to retrieve the current item? If it's from a state-changing notification, then the previous item may yet be selected.

      ---

      Shog9 If I could sleep forever, I could forget about everything...

      D 1 Reply Last reply
      0
      • S Shog9 0

        Where are you trying to retrieve the current item? If it's from a state-changing notification, then the previous item may yet be selected.

        ---

        Shog9 If I could sleep forever, I could forget about everything...

        D Offline
        D Offline
        dennisV
        wrote on last edited by
        #3

        I'm trying to get this in the OnLvnKeydown by responding to this: ON_NOTIFY(LVN_KEYDOWN, IDC_LIST, OnLvnKeydown) (and in my previous code I of course meant cList.GetNextItem(), instead of GetNextItem() - I'm not trying to get this from inside my derived class). MCP, MCSD

        S 1 Reply Last reply
        0
        • D dennisV

          I'm trying to get this in the OnLvnKeydown by responding to this: ON_NOTIFY(LVN_KEYDOWN, IDC_LIST, OnLvnKeydown) (and in my previous code I of course meant cList.GetNextItem(), instead of GetNextItem() - I'm not trying to get this from inside my derived class). MCP, MCSD

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          Ok, that makes sense now. Unless you specifically need to pay mind to keyboard selection while excluding all other forms, it would be easier for you to use the notification LVN_ITEMCHANGED and watch for changes to the selection state:

          void CMyWin::OnItemchangedListCtrl(NMHDR* pNMHDR, LRESULT* pResult)
          {
          NMLISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
          *pResult = 0;
          if ( pNMListView->iItem < 0 )
          return;

          if ( (pNMListView->uOldState & LVIS_SELECTED)
          && !(pNMListView->uNewState & LVIS_SELECTED) )
          {
          // item has lost selection
          }
          else if ( !(pNMListView->uOldState & LVIS_SELECTED)
          && (pNMListView->uNewState & LVIS_SELECTED) )
          {
          // item has gained selection
          }
          }

          ---

          Shog9 If I could sleep forever, I could forget about everything...

          D 1 Reply Last reply
          0
          • S Shog9 0

            Ok, that makes sense now. Unless you specifically need to pay mind to keyboard selection while excluding all other forms, it would be easier for you to use the notification LVN_ITEMCHANGED and watch for changes to the selection state:

            void CMyWin::OnItemchangedListCtrl(NMHDR* pNMHDR, LRESULT* pResult)
            {
            NMLISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
            *pResult = 0;
            if ( pNMListView->iItem < 0 )
            return;

            if ( (pNMListView->uOldState & LVIS_SELECTED)
            && !(pNMListView->uNewState & LVIS_SELECTED) )
            {
            // item has lost selection
            }
            else if ( !(pNMListView->uOldState & LVIS_SELECTED)
            && (pNMListView->uNewState & LVIS_SELECTED) )
            {
            // item has gained selection
            }
            }

            ---

            Shog9 If I could sleep forever, I could forget about everything...

            D Offline
            D Offline
            dennisV
            wrote on last edited by
            #5

            Yeah, that code works, thanks. I was watching for the OnItemChanged in another list I had, but that was to process checkmarks, for some reason I didn't think about watching for selection as well. :(( Could've saved me some time :cool: But I really don't understand why it doesn't process the OnKeyDown correctly. MCP, MCSD

            S 1 Reply Last reply
            0
            • D dennisV

              Yeah, that code works, thanks. I was watching for the OnItemChanged in another list I had, but that was to process checkmarks, for some reason I didn't think about watching for selection as well. :(( Could've saved me some time :cool: But I really don't understand why it doesn't process the OnKeyDown correctly. MCP, MCSD

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #6

              dennisV wrote: But I really don't understand why it doesn't process the OnKeyDown correctly. Most likely it sends the notification *before* it does it's own processing, thus the selection doesn't get changed until after your notification handler returns. Couldn't say why they did it that way, but you're usually better off not using it for selection changes anyway, as it'll ignore the mouse.

              ---

              Shog9 If I could sleep forever, I could forget about everything...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups