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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use a list ctrl to select an item

How to use a list ctrl to select an item

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
3 Posts 3 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.
  • C Offline
    C Offline
    Chiman1
    wrote on last edited by
    #1

    I am using a CListCtrl control in may application, where i have 10 iteams in that control. Now after receiving some input from user , i need to select a particular item from that list. I have a code where after receiving users input , i am calling myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED ); to select respective item from that ctrl. Now it is observed that after executing above line of code, afx_msg void OnSelchanged( NMHDR* pNMHDR, LRESULT* pResult ) is getting called multiple times. Also when i tried to retrieve selected item in OnSelchanged method , by using ( GetFirstSelectedItemPosition() and GetNextSelectedItem() ), it gives different output every time. Can you please explain, i) why this OnSelchanged() method is getting called multiple times whenever myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED );line of code gets executed. ii) Why i am getting different output ( id of item selected) after executing GetFirstSelectedItemPosition() and GetNextSelectedItem().

    D _ 2 Replies Last reply
    0
    • C Chiman1

      I am using a CListCtrl control in may application, where i have 10 iteams in that control. Now after receiving some input from user , i need to select a particular item from that list. I have a code where after receiving users input , i am calling myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED ); to select respective item from that ctrl. Now it is observed that after executing above line of code, afx_msg void OnSelchanged( NMHDR* pNMHDR, LRESULT* pResult ) is getting called multiple times. Also when i tried to retrieve selected item in OnSelchanged method , by using ( GetFirstSelectedItemPosition() and GetNextSelectedItem() ), it gives different output every time. Can you please explain, i) why this OnSelchanged() method is getting called multiple times whenever myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED );line of code gets executed. ii) Why i am getting different output ( id of item selected) after executing GetFirstSelectedItemPosition() and GetNextSelectedItem().

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Member 4253145 wrote:

      Can you please explain, i) why this OnSelchanged() method is getting called multiple times whenever myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED );line of code gets executed.

      Because the state of more than one item is being changed.

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      1 Reply Last reply
      0
      • C Chiman1

        I am using a CListCtrl control in may application, where i have 10 iteams in that control. Now after receiving some input from user , i need to select a particular item from that list. I have a code where after receiving users input , i am calling myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED ); to select respective item from that ctrl. Now it is observed that after executing above line of code, afx_msg void OnSelchanged( NMHDR* pNMHDR, LRESULT* pResult ) is getting called multiple times. Also when i tried to retrieve selected item in OnSelchanged method , by using ( GetFirstSelectedItemPosition() and GetNextSelectedItem() ), it gives different output every time. Can you please explain, i) why this OnSelchanged() method is getting called multiple times whenever myCtrl.SetItemState( itemID, LVIS_SELECTED, LVIS_SELECTED );line of code gets executed. ii) Why i am getting different output ( id of item selected) after executing GetFirstSelectedItemPosition() and GetNextSelectedItem().

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        When you select an item in the list control, the previously selected item is deselected. The OnSelChanged will be called twice here, once when the previous item is deselected and then when the new item is selected. This is what David mentioned in his reply. It is not a good idea to call functions like GetFirstSelectedItemPosition and GetNextSelectedItem inside the OnSelChanged handler. In-fact you don't need to. It is already given to you in the NMLISTVIEW parameter of the handler. You need to check the state of the uNewState member. if (LVIS_SELECTED & pNMLV->uNewState) If the above condition is true, you can get the selected item in the iItem member.

        «_Superman_» I love work. It gives me something to do between weekends.

        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