Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Example of multiple selection in CListBox

Posted on 1998-01-27
4
Medium Priority
?
1,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
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 …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

688 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