Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Example of multiple selection in CListBox

Posted on 1998-01-27
4
Medium Priority
?
1,267 Views
Last Modified: 2013-11-19
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.    

0
Comment
Question by:dombmark
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
AlunCarp earned 150 total points
ID: 1314983
dombmark

Use GetSelCount to retrieve the number of selected items in the list.

Using the return value allocate enough memory to hold that many ints.

Then call GetSelItems passing the count returned previously returned by GetSelCount and a pointer to the allocated memory block.


// Handle button click to get the items from the list
// GlobalAllocPtr is a macro defined in WINDOWSX.H
// Uses GlobalAlloc and GlobalLock to give you a pointer
// GlobalFreePtr is also in WINDOWSX.H
void CTestDlg::OnGetItems()
{
    int nCount = m_ListBox.GetSelCount();          // get a count of the selected item in the list box
    LPINT lpItems = (LPINT) GlobalAllocPtr( GPTR, sizeof( int ) * nCount );
    if( lpItems )
    {
        if( m_ListBox.GetSelItems( nCount, lpItems ) != LB_ERR )  // retrieve the selected items indexes
        {
            //
            // Process the selected items here
            //
        }
        else
            MessageBox( "Failed to Get Items", "Test OnGetItems", MB_OK );
     
        GlobalFreePtr( lpItems );
    }
    else
        MessageBox( "Failed to allocate memory", "Test OnGetItems", MB_OK );
}

// end sample

I haven't tested the above but it should work
0
 

Author Comment

by:dombmark
ID: 1314984
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?  



0
 

Expert Comment

by:AlunCarp
ID: 1314985
/*********************************************************/
/* 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                                       */
/*********************************************************/

0
 

Author Comment

by:dombmark
ID: 1314986
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!  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question