Solved

Example of multiple selection in CListBox

Posted on 1998-01-27
4
1,246 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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now