MFC List Control: How to get selected items in a list control?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
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
    Location
    Switzerland
    Posts
    4,443

    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 05: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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center