Announcement

Collapse
No announcement yet.

Need help on simple LISTBOX GET COUNT

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

  • Need help on simple LISTBOX GET COUNT

    I'm a newbie and can't seem to get the "LISTBOX GET COUNT" statement to report the number of items slected in my listbox. Searched everywhere for examples and couldn't find any on Google, forum, \samples, etc.

    #PBFORMS CREATED V1.51
    #COMPILE EXE
    #DIM ALL

    '--------------------------------------------------------------------------
    ' ** Includes **
    '--------------------------------------------------------------------------
    #PBFORMS BEGIN INCLUDES
    #IF NOT %DEF(%WINAPI)
    #INCLUDE "WIN32API.INC"
    #ENDIF
    #PBFORMS END INCLUDES
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** Constants **
    '--------------------------------------------------------------------------
    #PBFORMS BEGIN CONSTANTS
    %IDD_DEFRAG = 101
    %IDC_LABEL1 = 1005
    %IDC_OK = 1006
    %IDC_CANCEL = 1007
    %IDC_DRIVESELECTLISTBOX = 1001
    #PBFORMS END CONSTANTS
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** Declarations **
    '--------------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
    DECLARE FUNCTION DriveSelectListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL _
    lCount AS LONG) AS LONG
    DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    #PBFORMS DECLARATIONS
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** Main Application Entry Point **
    '--------------------------------------------------------------------------
    FUNCTION PBMAIN()
    ShowDIALOG1 %HWND_DESKTOP
    END FUNCTION
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** CallBacks **
    '--------------------------------------------------------------------------
    CALLBACK FUNCTION ShowDIALOG1Proc()
    DIM hDlg AS DWORD
    DIM idItem AS LONG
    DIM datav AS LONG

    SELECT CASE AS LONG CBMSG
    CASE %WM_INITDIALOG
    ' Initialization handler

    CASE %WM_NCACTIVATE
    STATIC hWndSaveFocus AS DWORD
    IF ISFALSE CBWPARAM THEN
    ' Save control focus
    hWndSaveFocus = GetFocus()
    ELSEIF hWndSaveFocus THEN
    ' Restore control focus
    SetFocus(hWndSaveFocus)
    hWndSaveFocus = 0
    END IF

    CASE %WM_COMMAND
    ' Process control notifications
    SELECT CASE AS LONG CBCTL
    CASE %IDC_DriveSelectListBox

    CASE %IDC_OK
    LISTBOX GET COUNT hDlg, idItem TO datav 'NOT WORKING ! !
    MSGBOX STR$(datav)

    CASE %IDC_CANCEL
    DIALOG END CBHNDL, 0
    FUNCTION = 1

    END SELECT
    END SELECT
    END FUNCTION
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** Populate Listbox **
    '--------------------------------------------------------------------------
    FUNCTION DriveSelectListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lCount AS LONG) AS LONG
    LOCAL i AS LONG, buffer AS STRING, zDrive AS ASCIIZ * 32
    DIM sDrive(1:32) AS STRING

    ' Obtain a list of drives from the system
    buffer = STRING$(2048, $NUL)
    buffer = LEFT$(buffer, GetLogicalDriveStrings(LEN(buffer), BYVAL STRPTR(buffer)))

    FOR i = 1 TO PARSECOUNT(buffer, $NUL)-1
    zDrive = PARSE$(buffer, $NUL, i)
    IF GetDriveType(zDrive)=%DRIVE_FIXED THEN
    LISTBOX ADD hDlg, lID, LEFT$(zDrive,2)
    sDrive(i)= LEFT$(zDrive,2)
    END IF
    NEXT i

    END FUNCTION
    '--------------------------------------------------------------------------

    '--------------------------------------------------------------------------
    ' ** Dialogs **
    '--------------------------------------------------------------------------
    FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    LOCAL lRslt AS LONG

    #PBFORMS BEGIN DIALOG %IDD_DEFRAG->->
    LOCAL hDlg AS DWORD

    DIALOG NEW hParent, "Defragment Drive", 79, 70, 112, 144, %WS_POPUP OR _
    %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR _
    %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR _
    %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT _
    OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
    CONTROL ADD LISTBOX, hDlg, %IDC_DRIVESELECTLISTBOX, , 15, 25, 80, 75, _
    %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %WS_VSCROLL OR _
    %LBS_MULTIPLESEL OR %LBS_SORT OR %LBS_NOTIFY, %WS_EX_CLIENTEDGE OR _
    %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
    CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "Select drive(s) to degrag", 15, _
    10, 80, 10
    CONTROL ADD BUTTON, hDlg, %IDC_OK, "OK", 5, 110, 45, 25
    CONTROL ADD BUTTON, hDlg, %IDC_CANCEL, "Cancel", 60, 110, 45, 25
    #PBFORMS END DIALOG

    DriveSelectListBox hDlg, %IDC_DriveSelectListBox, 30

    DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt

    #PBFORMS BEGIN CLEANUP %IDD_DEFRAG
    #PBFORMS END CLEANUP

    FUNCTION = lRslt
    END FUNCTION
    '-------------------------------------------------------------------------

  • #2
    According to the online doc for PB/Win 9 (version in use not shown; 9x assumed since LISTBOX GET COUNT is not valid in 8x) .....
    LISTBOX GET COUNT hDlg, id& TO datav&

    The number of items in the LISTBOX is retrieved, and assigned to the long integer variable specified by datav&.
    .. LISTBOX GET COUNT does not return the number of selected items, it returns the total number of items in the listbox control.

    But 'CONTROL SEND CBHNDL, %ID_LISTBOX, LB_GETSELCOUNT, %NULL, %NULL' should do the trick.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Try using "%IDC_DriveSelectListBox" instead of "idItem".

      I don't see where 'idItem' is given any value, let alone the ID value for the listbox.
      Rod
      I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

      Comment


      • #4
        Per Michael, I substituted CONTROL SEND CBHNDL, %ID_LISTBOX, LB_GETSELCOUNT, %NULL, %NULL for LISTBOX GET COUNT hDlg, idItem TO datav, but LB_GETSELCOUNT always returns '0' no matter how many items I select.

        I tried Rod's advice to substitute %IDC_DriveSelectListBox for idItem but still get '0' returned for 'datav' variable. I hard-coded 'idItem" to equal 0, 1, or 2, (compiled three times) but datav always returns '0'.

        I'm using the new PBWin 9.0 compiler.

        Comment


        • #5
          Per Michael, I substituted CONTROL SEND CBHNDL, %ID_LISTBOX, LB_GETSELCOUNT, %NULL, %NULL for LISTBOX GET COUNT hDlg, idItem TO datav
          That wouldn't have compiled (%ID_LISTBOX undefined equate, LB_GETSELCOUNT undefined variable) , so that cannot be everything you did.

          In your original, your problem was that hDlg was zero in
          Code:
          LISTBOX GET COUNT hDlg, idItem TO datav
          hDlg here should be CBHNDL; or you needed to set the local variable Hdlg = CBHNDL before using it.

          (Well, that was your problem re getting zero. That would then have been returning the total number of items in the list control, which is not what you wanted).

          >I hard-coded 'idItem" to equal 0, 1, or 2, (compiled three times)

          Except your control's ID is 1001, not 0, 1, or 2

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            When I evaluate my handle 'hDlg', it always returns '0'. Am I dimensioning 'hDlg' in the wrong place?

            Comment


            • #7
              Here is your program modified to work in PB9

              John Tate
              Code:
              #PBFORMS CREATED V1.51
              #COMPILE EXE
              #DIM ALL
              
              '--------------------------------------------------------------------------
              ' ** Includes **
              '--------------------------------------------------------------------------
              #PBFORMS BEGIN INCLUDES
              #IF NOT %DEF(%WINAPI)
              #INCLUDE "WIN32API.INC"
              #ENDIF
              #PBFORMS END INCLUDES
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** Constants **
              '--------------------------------------------------------------------------
              #PBFORMS BEGIN CONSTANTS
              %IDD_DEFRAG = 101
              %IDC_LABEL1 = 1005
              %IDC_OK = 1006
              %IDC_CANCEL = 1007
              %IDC_DRIVESELECTLISTBOX = 1001
              #PBFORMS END CONSTANTS
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** Declarations **
              '--------------------------------------------------------------------------
              DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
              DECLARE FUNCTION DriveSelectListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL _
              lCount AS LONG) AS LONG
              DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
              #PBFORMS DECLARATIONS
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** Main Application Entry Point **
              '--------------------------------------------------------------------------
              FUNCTION PBMAIN()
              ShowDIALOG1 %HWND_DESKTOP
              END FUNCTION
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** CallBacks **
              '--------------------------------------------------------------------------
              CALLBACK FUNCTION ShowDIALOG1Proc()
              DIM hDlg AS DWORD
              DIM idItem AS LONG
              DIM datav AS LONG
              DIM datav2 AS LONG
              
              SELECT CASE AS LONG CBMSG
              CASE %WM_INITDIALOG
              ' Initialization handler
              
              CASE %WM_NCACTIVATE
              STATIC hWndSaveFocus AS DWORD
              IF ISFALSE CBWPARAM THEN
              ' Save control focus
              hWndSaveFocus = GetFocus()
              ELSEIF hWndSaveFocus THEN
              ' Restore control focus
              SetFocus(hWndSaveFocus)
              hWndSaveFocus = 0
              END IF
              
              CASE %WM_COMMAND
              ' Process control notifications
              SELECT CASE AS LONG CBCTL
              CASE %IDC_DriveSelectListBox
              
              CASE %IDC_OK
              LISTBOX GET COUNT CB.HNDL, %idc_driveselectlistbox TO datav 'NOT WORKING ! !
              'use cb.hndl in callback --cb.hndl = hDlg in dialog
              LISTBOX GET SELCOUNT CB.HNDL ,%idc_driveselectlistbox TO datav2
              MSGBOX "Number of items in listbox "+STR$(datav)+$CRLF + "number selected" + STR$(datav2)
              
              CASE %IDC_CANCEL
              DIALOG END CBHNDL, 0
              FUNCTION = 1
              
              END SELECT
              END SELECT
              END FUNCTION
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** Populate Listbox **
              '--------------------------------------------------------------------------
              FUNCTION DriveSelectListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lCount AS LONG) AS LONG
              LOCAL i AS LONG, buffer AS STRING, zDrive AS ASCIIZ * 32
              DIM sDrive(1:32) AS STRING
              
              ' Obtain a list of drives from the system
              buffer = STRING$(2048, $NUL)
              buffer = LEFT$(buffer, GetLogicalDriveStrings(LEN(buffer), BYVAL STRPTR(buffer)))
              
              FOR i = 1 TO PARSECOUNT(buffer, $NUL)-1
              zDrive = PARSE$(buffer, $NUL, i)
              IF GetDriveType(zDrive)=%DRIVE_FIXED THEN
              LISTBOX ADD hDlg, lID, LEFT$(zDrive,2)
              sDrive(i)= LEFT$(zDrive,2)
              END IF
              NEXT i
              
              END FUNCTION
              '--------------------------------------------------------------------------
              
              '--------------------------------------------------------------------------
              ' ** Dialogs **
              '--------------------------------------------------------------------------
              FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
              LOCAL lRslt AS LONG
              
              #PBFORMS BEGIN DIALOG %IDD_DEFRAG->->
              LOCAL hDlg AS DWORD
              
              DIALOG NEW hParent, "Defragment Drive", 79, 70, 112, 144, %WS_POPUP OR _
              %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR _
              %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR _
              %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT _
              OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
              CONTROL ADD LISTBOX, hDlg, %IDC_DRIVESELECTLISTBOX, , 15, 25, 80, 75, _
              %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %WS_VSCROLL OR _
              %LBS_MULTIPLESEL OR %LBS_SORT OR %LBS_NOTIFY, %WS_EX_CLIENTEDGE OR _
              %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
              CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "Select drive(s) to degrag", 15, _
              10, 80, 10
              CONTROL ADD BUTTON, hDlg, %IDC_OK, "OK", 5, 110, 45, 25
              CONTROL ADD BUTTON, hDlg, %IDC_CANCEL, "Cancel", 60, 110, 45, 25
              #PBFORMS END DIALOG
              
              DriveSelectListBox hDlg, %IDC_DriveSelectListBox, 30
              
              DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
              
              #PBFORMS BEGIN CLEANUP %IDD_DEFRAG
              #PBFORMS END CLEANUP
              
              FUNCTION = lRslt
              END FUNCTION

              Comment


              • #8
                Thanks guys so much for the help. My finalized project for a hard drive defragmenter front-end is posted here: http://www.powerbasic.com/support/pb...838#post295838

                Comment

                Working...
                X