CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 1 of 1

Thread: MFC List Control: How to get selected items in a list control?

  1. #1
    Join Date
    Jun 2001

    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.

    // In your header file
    CStringList m_strList;
    // In your .cpp-file
      // Retrieve the numbers of selected items in a list control
      int nSelCount = m_MyListControl.GetSelCount(); 
      // Clear the string list
        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
        // 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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)