-
February 14th, 2003, 08:38 AM
#1
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.
Code:
// In your header file
CStringList m_strList;
// In your .cpp-file
CListDlg::OnSelChangeMyListControl()
{
// Retrieve the numbers of selected items in a list control
int nSelCount = m_MyListControl.GetSelCount();
// Clear the string list
m_strList.RemoveAll();
if(nSelCount)
{
CString str;
// Create and int array to store the indexes and initialze
// with the indexes of selected items
LPINT pItems = new int[nSelCount];
m_MyListControl.GetSelItems(nSelCount, pItems);
// 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
m_MyListControl.GetText(pItems[iIndex],str);
m_strList.AddTail(str);
}
// Tidy up the 'int' array
delete [] pItems;
}
}
All selected items are stored in the 'CStringList' variable. Do whatever you want now with the selected items.
Last edited by Andreas Masur; December 11th, 2005 at 06:46 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|