February 14th, 2003, 07:38 AM
MFC List Control: How to get selected items in a list control?
Q: How to get selected items in a list control?
A: Create a member variable of type 'CStringList' in your class. Add an 'LBN_SELCHANGE' message handler for you list control. This should create a member function 'OnSelChange..()'. This function must establish how many items are selected, and then retrieve the text of each one and add it to the your 'CStringList' member variable. Finally you can use the variable to get all selected items.
All selected items are stored in the 'CStringList' variable. Do whatever you want now with the selected items.
// In your header file
// In your .cpp-file
// Retrieve the numbers of selected items in a list control
int nSelCount = m_MyListControl.GetSelCount();
// Clear the string list
// Create and int array to store the indexes and initialze
// with the indexes of selected items
LPINT pItems = new int[nSelCount];
// Fills the array from the control
for(int iIndex = 0; iIndex < nSelCount; iIndex++)
// Retrieve the selected item text and store it in a
// string list
// Tidy up the 'int' array
delete  pItems;
Last edited by Andreas Masur; December 11th, 2005 at 05:46 AM.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.