Announcement

Collapse
No announcement yet.

Listbox: use of %LB_GETSELITEMS

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Listbox: use of %LB_GETSELITEMS

    I have a listbox with LB_MULTIPLESEL enabled, so one can select
    more than 1 item at the time.
    Does anybody have some example sourcecode of reading the pointers
    to the listbox items that are selected? I think it is done by sending
    the %LB_GETSELITEMS message. It sends back an array of pointers, but
    it's not clear to me how to store and process this array...
    Kind regards
    Eddy


    ------------------
    raimundo4u[email protected]
    Eddy

  • #2
    I found a VB example.. Will convert it to PB and try it out...
    Code:
       Option Explicit 
    
        Public Const LB_SETSEL = &H185& 
        Public Const LB_GETSELCOUNT = &H190 
        Public Const LB_GETSELITEMS = &H191 
    
        Public Declare Function SendMessageLong Lib _ 
              "user32" Alias "SendMessageA" _  
              (ByVal hwnd As Long, _ 
              ByVal wMsg As Long, _  
              ByVal wParam As Long, _  
              ByVal lParam As Long) _ 
              As Long 
    
        Public Declare Function SendMessageArray Lib _ 
              "user32" Alias "SendMessageA" _  
              (ByVal hwnd As Long, _ 
              ByVal wMsg As Long, _ 
              ByVal wParam As Long, _  
               lParam As Any) _ 
              As Long 
    
        '  When lParam equals -1, setting wParam to False deselects all 
        '  selections and setting to True selects all entries. 
        '  Setting lParam to any other number selects or deselects 
        '  the index specified, as per the wParam setting. 
    
    Subroutines in Form Containing List: 
    
        '-------------------------------------------- 
        Public Sub ReturnSelectedIndexes (LB As Control) 
           Dim SizeOfSelectedArray As Long 
           Dim ret As Long 
           ReDim SelectedArray(1 To 1) As Long 
    
           SizeOfSelectedArray = SendMessageArray(LB.hwnd, _ 
               LB_GETSELCOUNT, 0, 0) 
           ReDim SelectedArray(1 To SizeOfSelectedArray) As Long 
           ret = SendMessageArray(LB.hwnd, _ 
               LB_GETSELITEMS, SizeOfSelectedArray, SelectedArray(1)) 
        End Sub 
        '-------------------------------------------- 
        Public Sub ClearList (LB As Control) 
           Dim ret As Long 
           r = SendMessageLong(LB.hwnd, LB_SETSEL, False, -1) 
        End Sub 
        '-------------------------------------------- 
        Public Sub SelectAll (LB As Control) 
           Dimret As Long 
           r = SendMessageLong(LB.hwnd, LB_SETSEL, True, -1) 
        End Sub 
        '--------------------------------------------


    ------------------
    [email protected]
    Eddy

    Comment


    • #3
      The array must be LONG's and should be used together with
      LB_GETSELCOUNT, something like:
      Code:
        LOCAL lRes AS LONG
        lRes = SendMessage(hList, %LB_GETSELCOUNT, 0, 0)
        IF lRes AND lRes <> %LB_ERR THEN
           REDIM Larry(lRes - 1) AS LONG
           lRes = SendMessage(hList, %LB_GETSELITEMS, lRes, VARPTR(Larry(0)))
           'Larry now contains the items that are selected
        END IF
      Added "IF lRes THEN" to sample, for safety..
      ------------------




      [This message has been edited by Borje Hagsten (edited April 05, 2001).]

      Comment


      • #4
        Eddy;

        I find using

        Code:
        LastIndex&=SendMessage(hCtrl, %LB_GETCOUNT, 0,0)-1
        IF LastIndex&>=0 THEN
           FOR N&=0 TO LastIndex&
              IF SendMessage(hCtrl, %LB_GETSEL, N&, 0)>0 THEN
                  ' do something with the item which is selected 
              END IF
           NEXT N&
        END IF
        works well since I can process the items one at a time without resorting
        to storing stuff in an array.

        It works for me !


        ------------------
        Chris Boss
        Computer Workshop
        Developer of "EZGUI"
        http://cwsof.com
        http://twitter.com/EZGUIProGuy

        Comment


        • #5
          For small lists, okay. To keep track of a list with 10,000 items,
          %LB_GETSELITEMS makes big difference, especially on slow machines.
          Remember that the average user machine is <200MHz..


          ------------------

          Comment


          • #6
            Chris,
            Yes, your method works, but as Borje said, it could take some time
            searching large lists.
            In the meantime, I found that one can use 3 'methods' to access a
            listbox (or any other control):
            - sending messages with CONTROL SEND
            - sending messages with the API 'SendMessage()'-function
            - or using the predefined functions in 'Commctrl.inc'
            I think I must choose 1 of the 3 to make my source code look consequent.
            The 'Commctrl.inc'-functions look quite handy....
            Eddy


            ------------------
            [email protected]
            Eddy

            Comment

            Working...
            X