ex.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SUB Statement Example
                        Example                Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
' This example demonstrates buffered input. User input is allowed to the
' screen, but the total number of typed or input characters is limited.
' The computer beeps if you exceed the limit.
 
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
 
 DECLARE SUB BufInput (StringVar$, Limit%)
 
 CLS                                   ' Clear the screen
 Limit% = 10 'Number of characters to input
 
 LOCATE 10, 5                          ' Prompt for the input string
 PRINT "PLEASE INPUT A STRING OF TEN CHARACTERS: ";
 COLOR 0, 7: PRINT SPACE$(Limit%)
 LOCATE 10, 46
 
 CALL BufInput(a$, Limit%)             ' Input a buffered string
 
 COLOR 7, 0
 CLS
 
 LOCATE 10, 10                         ' Print the result
 PRINT "THE STRING YOU INPUT WAS: ";
 COLOR 0, 7
 PRINT a$
 COLOR 7, 0
 END
 
 SUB BufInput (StringVar$, Limit%)
 BackSp$ = CHR$(8)
 Enter$ = CHR$(13)
 Length% = 0
 
 'Loop waiting for input. ENTER terminates the routine.
 WHILE char$ <> Enter$
 char$ = INKEY$
    IF char$ <> "" THEN
      IF (Length% < Limit%) OR (char$ = BackSp$) THEN
         SELECT CASE char$
         CASE " " TO "~"  ' Input printable characters only
            StringVar$ = StringVar$ + char$
            PRINT char$;
            Length% = Length% + 1
         CASE BackSp$  'If BackSpace is pressed, erase.
           IF Length% <> 0 THEN
              Length% = Length% - 1
              StringVar$ = LEFT$(StringVar$, Length%)
              CurrX% = CSRLIN
              CurrY% = POS(0) - 1
              LOCATE CurrX%, CurrY%
              PRINT " "
              LOCATE CurrX%, CurrY%
           END IF
         END SELECT
      ELSE
         PLAY "o1AL32"
      END IF
    END IF
 WEND
 
 END SUB