# CodeGuru Technical FAQs > CodeGuru Individual FAQs >  MFC List Control: How to get selected items in a list control?

## Gabriel Fleseriu

*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.



```
// 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.

----------

