Category : BASIC Source Code
Archive   : QBTUTOR.ZIP
Filename : TUTOR-03.BAS

 
Output of file : TUTOR-03.BAS contained in archive : QBTUTOR.ZIP
goto example1
' The QuickBASIC Tutorial Series

' Part 3
' Keyboard Input & Screen Handling

' Copyright 1986 Ford Software

' 4845 Willowbend Blvd.
' Houston, TX 77035
' (713) 721-5205
' CompuServe ppn: 71355,470

'This Tutorial may not be copied or distributed in any form - printed, on disk
'or electronically - without the express written permission of Ford Software.
' Unlicensed distribution is a violation of copyright law
' and may be subject to civil and criminal prosecution.

'QuickBASIC is the registered trademark of the Microsoft Corporation.
'Ford Software is not associated in any way with the Microsoft Corporation.

'(Make sure NumLock is off and press PgDn)
'page 2
' I N T R O D U C T I O N

'This lesson will cover keyboard input and working with the screen. It will
'not cover graphics because that is a specialized and more advanced topic.

'To run examples of code in this and other lessons, first press PgUp to get to
'the opening screen. The first line of the screen will say "goto example#".
'Change it to the example number you wish to run. Press Ctrl-R to compile and
'run the code. When done, press Ctrl-F and enter "page " plus the page number
'you were on. You will have to use Ctrl-Up and -Down to realign the page on
'the screen correctly. Study the code and press Ctrl-R to see it run again.
'This is the best way to learn. If you don't undertand something, turn to that
'part of your QuickBASIC manual for more information.

'You should also feel free to try typing in code of your own anytime you wish
'to aid in your understanding. Insert your code before the END statement of
'the example you are running. You can also delete or modify the example if you
'wish. You can always reload this file from the disk if you mess up what is in
'memory.
'
'(Press PgDn)
'page 3
' Contents

' page
' 4 INPUT Command
' 5 LINE INPUT Command
' 6 INKEY$ Function
' 7 Printing to the Screen
' 8 PRINT USING Command
' 9 LOCATE Command
' 10 Turning Cursor Off/On
' 10 Changing Size of Cursor
' 11 CLS Command
' 11 CSRLIN Function
' 11 POS(0) Function
' 11 COLOR Command




' To search for specific topics, use Ctrl-F.
'(Press PgDn)
'page 4 INPUT Command

'INPUT is the quickest and easiest way to get input from the keyboard, but
'allows the programmer very little control over how data is entered or what
'type of data is entered.
example1:
INPUT "Enter first name"; NA1$ 'This will print "Enter first name?_"
INPUT "Enter last name:", NA2$ 'Use a comma after the prompt for no "?".
INPUT "Enter Month, Day, Yr:", MO, DY, YR 'Multi-variable input is the major
'weakness of the INPUT command. If the user doesn't respond with the correct
'number of fields seperated by the right number of commas, he gets the not
'very helpful error message "?Redo from start" and the input field drops down
'to the next line, messing up any fancy screen layouts you might have had.
'This one weakness keeps most good programmers from ever using INPUT.
'Another problem is if the user does not enter the right type of data. You get
'another sloppy, cryptic error message if you try to enter text when the
'program wants numbers, as above. The programmer can help by describing on
'the screen exactly what he wants, but that is not very efficient:
INPUT "Enter Month, Day, Yr in the format 10, 15, 1986: ", MO, DY, YR
'Also, seperating data by commas is not how people usually write.
END
' (PgDn)
'page 5 (INPUT, continued)

'You can get around that problem in a round-about way:
example2:
LOCATE 10,11 : PRINT "MM/DD/YY"; ' This displays the format to be used.
LOCATE 10,11 : INPUT "", X$ ' The comma suppresses the question mark.
MO$=LEFT$(X$,2) 'Now you can pull the month, day and year out. But life
DY$=MID$(X$,4,2) 'isn't really this easy. You will have to test for X$
YR$=RIGHT$(X$,2) 'being the right length, the "/" being in the right place,
'and so forth - not a very efficient procedure.
END

'LINE INPUT
'Since commas are treated by INPUT as seperating data for different variables,
'you need a way of entering data that will not be bothered by commas and other
'things that upset the INPUT command. LINE INPUT is the command to do it.
'Also, LINE INPUT does not add a question mark to your prompt as INPUT does.
example3:
INPUT "Try to enter text with some commas (just press Enter to give up): ", X$

LINE INPUT "Now try again: "; X$
END
' (PgDn)
'page 6 INKEY$

'INKEY$ gets a keypress from the keyboard buffer, if any is there. If you have
'not already pressed a key, INKEY$ does not wait for you to do so like INPUT
'does, so when using INKEY$ to get input, you must use a loop:

example4: IN$=""
PRINT "Enter text:" 'Don't worry about this next line now. It just says to
WHILE IN$<>CHR$(13) ' repeat the loop until the Enter key is pressed.
IN$="" 'You must make sure the variable is clear before starting
WHILE IN$="" ' the loop or the loop may not execute.
IN$=INKEY$ 'You must assign to a variable the keypress returned by
WEND ' the INKEY$ function.
PRINT IN$; 'INKEY$ doesn't echo to the screen. You must explicitly
WEND ' print the character. Notice the semicolon after the
PRINT "Done" ' variable being printed. That makes each letter follow
END ' the last instead of being on a line by itself.

'The INKEY$ function is the basis for a sophisticated data entry routine that
'will be examined in a later lesson.

' (PgDn)
'page 7 Printing to the Screen

'PRINT...
' is the simplest method to print to the screen.
example5:
PRINT "123456789-123456789-" '(Read about TAB, SPC and SPACE in the manual.)
PRINT TAB(20) "XXXXX" 'TAB(20) skips to the 20th character position
PRINT SPC(20) "yyyyy" 'skips 20 spaces and starts in the 21st position
PRINT SPACE$(20) "ZZ" 'also skips 20 spaces

PRINT "semi"; 'A semicolon means that the next print command will start at
PRINT "colon" 'the very next character position instead of the next line.
PRINT "se"; "mi"
PRINT "col" "on" 'The semicolon is optional within a PRINT statement.

PRINT "comma", 'A comma between fields will cause the next print command to
PRINT "tab" 'skip to the next tab position.
PRINT "tab 1", 2, 3, 4, 5, 6, 7, 8 'This shows the tab positions
END
'Press Ctrl-R to compile and run this example, then compare to the code above.

' (PgDn)
'page 8 PRINT USING...

' lets you format the output to the screen. Although there are a whole lot of
'fancy formatting options listed under PRINT USING in the manual, 99% of the
'time, you will be using PRINT USING to format numeric output. The procedure
'is really very simple.
example6: ' EXPLANATIONS:
X=10/3 ' Set X equal to 3.333333
PRINT X ' This is the unformatted output.
PRINT USING "#####"; X ' This truncates the decimal.
PRINT USING "###.#"; X; 2 ' Prints X and 2 to one decimal place.
PRINT USING "##.##"; X; 3 ' Prints X and 3 to two places.
PRINT USING "#.###"; X; 4 ' This will look like "3.3334.000",
PRINT USING "#.### "; X; 4 ' so we put some space in between.
PRINT USING "$##.## "; X; 5; X*2 ' Most text imbedded in a PRINT USING
PRINT USING "#.## Dollars "; X; X+X ' string will print as is.
X$="##.## with variable" ' The PRINT USING string can be
PRINT USING X$; X ' assigned to a variable.
END
'To run this, press Ctrl-PgUp, change to "goto example6", press Ctrl-R.

' (PgDn)
'page 9 LOCATE

'LOCATE is used to make printing begin at a specific point on the screen. The
'format is "LOCATE x,y", where "x" is the row number (1-25) and y is the
'column (or horizontal character position) number (1-80).
'(As usual, press Ctrl-PgUp, change to "goto example7", press Ctrl-R to run.)

example7:
LOCATE 24,38:PRINT"1: 24,38" 'Printing on lines 24 or 25 without a trailing
LOCATE 24,38:PRINT"2: 24,38;"; 'semicolon makes the screen scroll.
LOCATE 1,15 :PRINT"3: 1,15"; 'Print starts at row 1, column 15.
LOCATE 2 :PRINT"4: 2"; 'Leaving off the column number starts at the
'normal column number on the specified line, in
'this case, the end of the last text printed.
LOCATE ,60 :PRINT"5: ,60" 'Leaving off the row number starts print at
'the normal row, in this case, row 2, since the
'last PRINT statement ended with a semicolon.
LOCATE ,50 :PRINT"6: ,50" 'This will print on row 4, since the last
'PRINT statement did not end with a semicolon.
END

' (PgDn)
'PAGE 10 (LOCATE, continued)

'Other LOCATE Functions:

'LOCATE 10,30,0 positions the cursor at row 10, column 30, and turns the
'blinking cursor off. If you just want to toggle the cursor on and off, leave
'out the row and column numbers:
example8:
LOCATE ,,0 : PRINT "The cursor is off. Press ENTER";
X$="":WHILE X$="":X$=INKEY$:WEND
LOCATE 2,1,1 : PRINT "The cursor is on. Press ENTER";
X$="":WHILE X$="":X$=INKEY$:WEND
LOCATE 3,1
'Cursor size can be changed with the fourth and fifth positions of LOCATE:
WHILE TOP<>9
INPUT "Top scan line # for cursor (0-7, 9 TO END)"; TOP
IF TOP=9 THEN END
INPUT "Bottom scan line # for cursor (0-7)"; BOT
LOCATE ,,,TOP,BOT : PRINT
WEND 'The most common reason to change the cursor size is to indicate if
'the user is in the Insert or Overtype mode.
' (PgDn)
'page 11 Other Video Commands

'CLS, of course, clears the screen.

'CSRLIN tells you what line the cursor is on. Eg: ROW=CSRLIN

'POS(0) tells you the horizontal position of the cursor. Eg: COL=POS(0)

'COLOR x,y,z changes screen colors. The following code will let you try
' different colors. (Border won't work on EGA, so we left it out.)

example9:
WHILE FG <> 99
INPUT "Foreground (0-31, 99 to Quit)"; FG
IF FG=99 THEN END
INPUT "Background (0-7, not same as foreground)"; BG
IF FG<>BG THEN COLOR FG,BG
WEND



' end of lesson 3




'press PgUp


  3 Responses to “Category : BASIC Source Code
Archive   : QBTUTOR.ZIP
Filename : TUTOR-03.BAS

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/