Solved

Example of multiple selection in CListBox

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

837 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