Link to home
Start Free TrialLog in
Avatar of dombmark
dombmark

asked on

Example of multiple selection in CListBox

I'm using VC++ 5.0

I've never implemented multiple selection before in CListBox and am running into some difficulty.  I'm using the function (I think it is called) GetSelItems which takes an integer max number of selections to allow and an LPINT buffer which is supposed to receive the zero ordered array positions within the list box of the selected items.  This buffer is supposed to be "big enough to hold the max number of selectsion" but I'm not sure how to define it that way.  The function returns an integer which is the number of selected items.  

I can get the function to return the number of items, but I'm having no luck getting the LPINT buffer to give me the array positions of the selected items.  Can someone please provide some sample code for how to define the buffer and how to get the item positions out of this buffer.  I want to get these items so as to move them to another listbox.  I figure this isn't hard if you know what you are doing.  

Thanks in advance for any help.    

ASKER CERTIFIED SOLUTION
Avatar of AlunCarp
AlunCarp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dombmark
dombmark

ASKER

I'm not getting this code to work.  The nCount variable does get the correct number of selections in the list box.  The lpItems variable does get the correct index for the first selection.  the problem is that when more than one item is selected, I only have the first item index in lpItems, and not the rest of them.  
For example.  If I select items 4, 5 and 6 in a list box.  nCount correctly returns as 3, but lpItems returns only as 4.  There is no indication that 5 and 6 were selected based on the value of lpItems.  

Should lpItems be implemented as an array of size nCount?  how would you go about doing that?  

Any suggestions?  



/*********************************************************/
/* Insert this bit into the above code where appropriate */
/*********************************************************/

// allocate enough space to get the item indexes
// size is nCount * the size of an int (4 bytes in Win32)
// if nCount were 3 we would allocate 12 bytes here
LPINT lpItems = (LPINT) GlobalAllocPtr( GPTR, sizeof( int ) * nCount );
if( lpItems )
{
    if( m_ListBox.GetSelItems( nCount, lpItems ) != LB_ERR ) // retrieve the selected items indexes
    {
        int nLoop;
        CString strTemp;
    //
    // Process the selected items here
    // this is done by referencing lpItems as an array of ints
    // in this example nCount will be 3
        for( nLoop = 0; nLoop < nCount; nLoop ++ )
        {
            // process the list box item indicated by
            // lpItems[nLoop]
            // stuff it into a CString and display in a message box
            // quick and dirty debugging method :)
            strTemp.Format( "Selected item %d has index %d", nLoop, lpItems[nLoop] );
            MessageBox( strTemp, "Test OnGetItems", MB_OK );
        }
    }
}

/*********************************************************/
/* Hope this helps                                       */
/*********************************************************/

Okay - I got it now.  your code was fine.  The problem was that I was viewing lpItems in a watch window and I expected it's array nature to be apparent there.  It was not when I "watched" it as lpItems.  However, when I "watched" it as lpItems[0], lpItems[1], lpItems[2], etc.  I was able to see all the correct item indices. Thank you very much for your answer!