Announcement

Collapse
No announcement yet.

DDT/Getting selected items in Listbox OK?

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

  • DDT/Getting selected items in Listbox OK?

    Hi,

    I'm trying to write my first app with the DDT features. CONTROL GET TEXT works fine If I use a combobox, but doesn't return anything when using a listbox, even if I only select one item in my multiple-selection-capable listbox. Anybody got it to work?

    Code:
    CALLBACK FUNCTION OkButton()
      LOCAL sTemp AS STRING
    
      CONTROL GET TEXT CBHNDL, %IDLIST TO sTemp
    
      'Empty!
      MSGBOX sTemp 
    
      DIALOG END CBHNDL, 1
    END FUNCTION
    I checked the ADDRESS.BAS sample, and tried the following, but no more luck:

    Code:
    CONTROL SEND hDlg, %IDLIST,  %CB_GETCURSEL, STRPTR(sTemp), 0
    Thx
    FF.

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

  • #2
    Try this in the listbox callback.

    Code:
    CONTROL SEND CBHNDL , CBCTL, %LB_GETCURSEL, 0,0 TO CVal&
            
    IF CVal& => 0 THEN
        CONTROL SEND CBHNDL, CBCTL, %LB_GETTEXT, CVal&, STRPTR(sBuffer$)
        MSGBOX sBuffer$
    END IF
    ------------------
    -Greg

    [This message has been edited by Gregery D Engle (edited July 14, 2001).]
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    Comment


    • #3
      [QUOTE]Originally posted by Gregery D Engle:
      Try this in the listbox callback.

      Thx! Any idea why "CONTROL GET TEXT" wouldn't work with a listbox?

      I'd rather check that all fields are filled when the user clicks on OK instead on doing this when the listbox sends a message, though.

      Thx for the prompt answer
      FF.




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

      Comment


      • #4
        With DDT, you can also use:
        Code:
          LISTBOX GET TEXT CBHNDL, %IDLIST TO sTemp

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

        Comment


        • #5
          Originally posted by Frederic Faure:
          Thx! Any idea why "CONTROL GET TEXT" wouldn't work with a listbox?
          LISTBOX GET TEXT hDlg&, id& TO text$


          ------------------
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Comment


          • #6
            oops Börje, är Du fortfarande vaken
            Translated:Still awake Borje?

            ------------------
            Fred
            mailto:[email protected][email protected]</A>
            http://www.oxenby.se

            Fred
            mailto:[email protected][email protected]</A>
            http://www.oxenby.se

            Comment


            • #7
              Don't use SLEEP much. Think DOEVENTS is better..


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

              Comment


              • #8
                Cute! I must remember that line...

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Looks like I'm not the only one this side of the Pond doing an all-nightie over the week-end

                  FF.



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

                  Comment


                  • #10
                    One point overlooked is that Frederic is using a multi-selection
                    Listbox and not a single selection Listbox.

                    You can't simply request the selected text with a single statement.

                    You must first poll all the items in the listbox to see if they have
                    been selected and then request the text for each item that has been
                    selected.

                    Code:
                    LastI&=SendMessage(hCtrl, %LB_GETCOUNT,0,0)  ' get count of items
                    IF LastI&>0 THEN
                       LastI&=LastI&-1
                       FOR I&=0 to LastI&   ' zero indexed
                          S&=SendMessage(hCtrl, %LB_GETSEL, I&, 0)
                          IF S&<>0 THEN
                             TL&=SendMessage(hCtrl, %LB_GETTEXTLEN, I&, 0)
                             Text$=SPACE$(TL&+1)
                             ATL&=SendMessage(hCtrl, %LB_GETTEXT, I&, STRPTR(Text$))
                             Text$=LEFT$(Text$,ATL&)
                          END IF
                       NEXT I&
                    END IF

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

                    Comment


                    • #11
                      Originally posted by Chris Boss:
                      One point overlooked is that Frederic is using a multi-selection Listbox and not a single selection Listbox. [/B]
                      Indeed It only returned the last item selected in the list.

                      Thx again
                      FF.



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

                      Comment


                      • #12
                        So many ways to do things. A fast way to grab selected items from
                        multiple sel list is LB_GETSELITEMS. If list has few items, running
                        through all is okay, but if list has lots of items, following has
                        served me well:
                        Code:
                          LOCAL I AS LONG, lRes AS LONG, txt AS STRING
                         
                          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)))
                          END IF
                         
                          FOR I = 0 TO UBOUND(Larry)
                             lRes = SendMessage(hList, %LB_GETTEXT, Larry(I), STRPTR(txt))
                             'txt holds selected item's text - do what you like with it..
                          NEXT

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

                        Comment


                        • #13
                          Found following little sample in my drawer. Two lists, one with a thousand
                          items, the other empty, until user selects or de-selects something in filled
                          list, then selected items are placed in other list. Have been quite useful
                          to me in many situations, so here it goes:
                          Code:
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          ' Template
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          #COMPILE EXE
                          #INCLUDE "WIN32API.INC"
                          %ID_LIST  = 31
                          %ID_LIST2 = 32
                           
                          DECLARE CALLBACK FUNCTION DlgProc() AS LONG
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          ' Create dialog and controls, etc
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          FUNCTION PBMAIN () AS LONG
                            LOCAL hDlg AS LONG, I AS LONG
                            REDIM arr(1000) AS STRING
                            FOR I = 0 TO UBOUND(arr) : arr(I) = "Item number" & STR$(I) : NEXT
                           
                            DIALOG NEW 0, "Template",,, 237, 160, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
                            CONTROL ADD BUTTON, hDlg, 10, "E&xit", 180, 4,  50,  14
                            CONTROL ADD LABEL,  hDlg, 20, "Selected items:", 4, 15, 100,  10   '%LBS_MULTIPLESEL %LBS_EXTENDEDSEL
                            CONTROL ADD LISTBOX, hDlg, %ID_LIST,, 4,  25, 110, 140, %WS_CHILD OR %LBS_EXTENDEDSEL OR _
                                                 %WS_VSCROLL OR %LBS_DISABLENOSCROLL, %WS_EX_CLIENTEDGE CALL DlgProc
                           
                            CONTROL ADD LISTBOX, hDlg, %ID_LIST2, Arr(), 122,  25, 110, 140, %WS_CHILD OR %LBS_MULTIPLESEL OR _
                                                 %WS_VSCROLL OR %LBS_DISABLENOSCROLL, %WS_EX_CLIENTEDGE CALL DlgProc
                           
                            DIALOG SHOW MODAL hDlg CALL DlgProc
                           
                          END FUNCTION
                           
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          ' Main callback
                          '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          CALLBACK FUNCTION DlgProc() AS LONG
                            LOCAL I AS LONG, lRes AS LONG, hList AS LONG, hList2 AS LONG, txt AS STRING
                           
                            SELECT CASE CBMSG
                               CASE %WM_COMMAND
                                  IF CBCTL = 10 THEN
                                     DIALOG END CBHNDL
                           
                                  ELSEIF CBCTL = %ID_LIST2 THEN
                                     IF CBCTLMSG = %LBN_SELCHANGE THEN
                                        hList = GetDlgItem(CBHNDL,%ID_LIST)
                                        hList2 = GetDlgItem(CBHNDL,%ID_LIST2)
                                        lRes = SendMessage(hList2, %LB_GETSELCOUNT, 0, 0)
                           
                                        IF lRes AND lRes <> %LB_ERR THEN
                                           REDIM Larry(lRes - 1) AS LONG
                                           lRes = SendMessage(hList2, %LB_GETSELITEMS, lRes, VARPTR(Larry(0)))
                                        END IF
                           
                                        CALL SendMessage(hList, %LB_RESETCONTENT, 0, 0)
                                        FOR I = 0 TO UBOUND(Larry)
                                           lRes = SendMessage(hList2, %LB_GETTEXT, Larry(I), STRPTR(txt))
                                           lRes = SendMessage(hList, %LB_ADDSTRING, 0, BYVAL STRPTR(txt))
                                        NEXT
                                     END IF
                                  END IF
                           
                            END SELECT
                           
                          END FUNCTION

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

                          Comment


                          • #14
                            Thx everyone for the prompt help. I can successfully extract selected items in my list of hosts now.

                            Now, one day we'll have an OOP PB/DLL to make this kind of thing much easier...

                            FF.

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

                            Comment


                            • #15
                              How would OOP make this task trivial? Or are you meaning that you'd only have to use an object (created earlier by someone else) that exposes a suitable method, in order to retrieve the list of selected items with one line of code?

                              If so, there is no real difference to implementing the code posted above as a Sub or Function and calling that.



                              ------------------
                              Lance
                              PowerBASIC Support
                              mailto:[email protected][email protected]</A>
                              Lance
                              mailto:[email protected]

                              Comment


                              • #16
                                Originally posted by Lance Edmonds:
                                How would OOP make this task trivial? Or are you meaning that you'd only have to use an object (created earlier by someone else) that exposes a suitable method, in order to retrieve the list of selected items with one line of code?
                                Indeed. I spent over 3 hours trying to get the select strings from a list box, and w/out your help here, it could have taken me even more time until I got it. A cListBox object would have saved me that effort.
                                After all, the major point of capitalism is economy of scale... It doesn't make sense to go back to the Middle Ages when each craftsman would build his own tools instead of getting them all-made from a tool maker.

                                BTW, I already asked swhere else, but is there any will over at PowerBasic to make PB/DLL an OOP?

                                DIM cListBox as myLB
                                DIM sItem as STRING

                                myLB.Add "item1"
                                myLB.Add "item2"

                                sleep 10

                                sItem = myLB.GetSel
                                while sItem
                                msgbox sItem
                                sItem = myLB.GetSel
                                Wend

                                Thx
                                FF.



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

                                Comment


                                • #17
                                  PowerBASIC staff are not permitted to discuss feature sets for products that are not yet shipping. On that basis, I cannot answer your question, sorry!

                                  However, I can say that there have been quite a number of requests for things like COM. The higher that items appear on the wish list, the greater the chances that R&D will implement them in future product versions.



                                  ------------------
                                  Lance
                                  PowerBASIC Support
                                  mailto:[email protected][email protected]</A>
                                  Lance
                                  mailto:[email protected]

                                  Comment


                                  • #18
                                    I fail to see where so called OOP would make a thing like this easier.
                                    Using the messages directly, you get a considerable speed gain and it
                                    is no harder to address a control via its handle, than via a runtime
                                    wrapper.

                                    If you test the sample code I gave - a list with thousand items and direct
                                    response to what is selected or not in second list, and try the same in VB
                                    or Delphi, you'll soon discover that you will have to use same API calls
                                    to achieve a pleasant speed. Users will not like to have to wait while the
                                    runtime loops through all items each time they click in the list.

                                    I'm sure next version of PB will make things easier for us all, each
                                    version usually does, but no matter what - when it comes to powerful
                                    coding, there are no shortcuts in any language (though some languages,
                                    like VB, has made it a bit more difficult to use the API directly..


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

                                    Comment


                                    • #19
                                      Originally posted by Borje Hagsten:
                                      I fail to see where so called OOP would make a thing like this easier.
                                      The code did solve the issue (and thx for the help!), but it was just a general remark. I like OOP, because of the way it has to keep data really private, and to make it easier for developers to hide complexity, and share code.

                                      BTW, why would making PB an OOP Basic necessarily cause a significant speed loss? So far, I've only found one OOP Basic language (open-source Oasis). Is VB really the only game in town?

                                      FF.



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

                                      Comment

                                      Working...
                                      X