How to use a list ctrl to select an item
-
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().
-
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().
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
-
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().
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 likeGetFirstSelectedItemPosition
andGetNextSelectedItem
inside theOnSelChanged
handler. In-fact you don't need to. It is already given to you in theNMLISTVIEW
parameter of the handler. You need to check the state of theuNewState
member.if (LVIS_SELECTED & pNMLV->uNewState)
If the above condition is true, you can get the selected item in theiItem
member.«_Superman_» I love work. It gives me something to do between weekends.