'=========================================================================== ' Subject: PROGRESS BAR EXAMPLE Date: 12-03-99 (05:49) ' Author: Kenneth Ives Code: QB, QBasic, PDS ' Origin: kenaso@home.com Packet: MISC.ABC '=========================================================================== ' -------------------------------------------------------------- ' Written by Kenneth Ives kenaso@home.com ' If you find this useful, please give me credit. Thank you. ' ' Name of program: ProgBar.bas ' Overview: Progress bar demo ' Commandline parameters: None ' ' Modification History: ' DATE COMMENTS ' 11/30/1999 Wrote program using PDS 7.1 (Extended QuickBASIC) ' -------------------------------------------------------------- ' ' -------------------------------------------------------------- ' Sub/Function Declarations ' -------------------------------------------------------------- DECLARE SUB EmptyProgressBar (iBarRow AS INTEGER, iBarCol AS INTEGER, iMaxBlocks AS INTEGER) DECLARE SUB FillProgressBar (iBarRow AS INTEGER, iBarCol AS INTEGER, iMaxBlocks AS INTEGER, dCurrAmt AS DOUBLE, dMaxAmt AS DOUBLE) ' -------------------------------------------------------------- ' Define Variables ' -------------------------------------------------------------- DIM dTestAmt AS DOUBLE ' Make this double, Possible large numbers DIM n AS DOUBLE ' test counter DIM sKEY AS STRING ' single keystroke ' Progress bar variables DIM iRow AS INTEGER ' Row to display progress bar DIM iCol AS INTEGER ' Column to display progress bar DIM iNbrOfBlocks AS INTEGER ' Number of progress bar blocks ' -------------------------------------------------------------- ' Initialize variables ' -------------------------------------------------------------- iRow = 10 iCol = 15 iNbrOfBlocks = 50 dTestAmt = 145623 ' Example of a maximum value (possible file size) ' -------------------------------------------------------------- ' Start program ' -------------------------------------------------------------- CLS LOCATE 22, 1 PRINT SPACE$(25) + "Press ENTER to start demo" PRINT SPACE$(28) + "or ESCape to quit." sKEY = INPUT$(1) ' Wait for keyboard input IF CHR$(27) = sKEY THEN ' if ESC is pressed, leave CLS : END END IF TopOfLoop: n = 0 CLS ' Display empty progress bar EmptyProgressBar iRow, iCol, iNbrOfBlocks DO n = n + 1 ' Increment the counter ' Warning: do not put this IF on a single line ' because sometimes it will not exit IF n >= dTestAmt THEN EXIT DO END IF ' pass the row and column to display the progress bar ' the number of blocks to be displayed in the original ' bar along with the current count and max value allowed. ' As you can see, only "n" changes. The other values ' are constant for the duration of progress. ' FillProgressBar iRow, iCol, iNbrOfBlocks, n, dTestAmt LOOP LOCATE 22, 1 PRINT SPACE$(25) + "Press ENTER to start over" PRINT SPACE$(28) + "or ESCape to quit." sKEY = INPUT$(1) ' Wait for keyboard input IF CHR$(27) = sKEY THEN ' if ESC is pressed, leave CLS : END END IF GOTO TopOfLoop SUB EmptyProgressBar (iBarRow AS INTEGER, iBarCol AS INTEGER, iMaxBlocks AS INTEGER) STATIC ' ---------------------------------------------- ' display unused (empty) progress bar ' ---------------------------------------------- LOCATE iBarRow, iBarCol PRINT STRING$(iMaxBlocks, CHR$(177)) END SUB SUB FillProgressBar (iBarRow AS INTEGER, iBarCol AS INTEGER, iMaxBlocks AS INTEGER, dCurrAmt AS DOUBLE, dMaxAmt AS DOUBLE) STATIC ' ---------------------------------------------- ' Define local variables ' ---------------------------------------------- DIM dCalcAmt AS DOUBLE DIM iBlockCnt AS INTEGER DIM iBarProgress AS INTEGER DIM iAmtLeft AS INTEGER DIM iPerCent AS INTEGER DIM sPerCent AS STRING ' ---------------------------------------------- ' Calculate the percentage of the actual value ' based on the current value and the maximum ' allowable value. ' ---------------------------------------------- IF dCurrAmt >= dMaxAmt THEN dCurrAmt = dMaxAmt - 1 dCalcAmt = dMaxAmt - dCurrAmt iPerCent = (100 - INT(100 * dCalcAmt / dMaxAmt)) ' ---------------------------------------------- ' Make sure we do not display negative values ' or values greater than 100% ' ---------------------------------------------- IF iPerCent <= 0 THEN iPerCent = 1 ELSEIF iPerCent >= 100 THEN iPerCent = 100 END IF ' ---------------------------------------------- ' Reformat the percentage output display ' ---------------------------------------------- sPerCent = CHR$(32) + STR$(iPerCent) + "%" ' ---------------------------------------------- ' Calculate the progression of the progress bar ' ---------------------------------------------- iBlockCnt = INT((iMaxBlocks / 100) * (100 - iPerCent)) iBarProgress = iMaxBlocks - iBlockCnt ' ---------------------------------------------- ' Make sure we do not display negative values ' or values greater than 100% ' ---------------------------------------------- IF iBarProgress <= 0 THEN iBarProgress = 1 ELSEIF iBarProgress >= 100 THEN iBarProgress = 100 END IF ' ---------------------------------------------- ' display the used portion of the progress bar ' and the percentage ' ---------------------------------------------- LOCATE iBarRow, iBarCol: PRINT STRING$(iBarProgress, CHR$(219)) LOCATE iBarRow, (iBarCol + iMaxBlocks): PRINT sPerCent END SUB