Getting Started
Sample Programs
QBasic IDEs
Code Snippets

Web Sites
More Tutorials
User Groups
Talk Shows

GBIC >> QBasic >> Tutorial >> Screen - Text

QBasic Information Center Tutorials - Screen Text
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Screen - Text
QBasic is capable of displaying both text and graphics on the screen. In this section, the text output (screen mode 0) features are discussed.

    • Screen/Mode Properties    
     width, view print, screen, color 
    • Cursor Status
     csrlin, pos, locate 
    • Output to Screen
     print, print using, write 
    • Print Arguments
     tab, spc 

QBasic supports several screen modes, which affect the resolution, color and display of text (yes, you can write text on the screen). The default screen mode, and perhaps the most commonly used, is screen mode 0 which is a text only mode. Screen mode 0 has a default size of 80 columns by 25 rows. The text display boundaries may be modified using the width and view print statements.

The print/print using/write statements are used to write text onto the screen, each with a different approach to formatting the output. The output of all three can tailored using the tab/spc functions. Color foreground and background of the screen is set using the color statement.

Writing takes places at the current cursor location, which is reset under user control at the end of a print/print using/write statement. The cursor position may also be explicitly set using the locate statement.

Information about the screen (cursor position and screen content) can be retrieved using the csrlin, pos, and screen functions.

Text Viewport
As with graphics, a text viewport can also be create on a screen. The text and graphics viewports are independent of one another.

Normally, a QBasic program can write anywhere on the screen, over the entire width/column settings for the screen mode. However, QBasic also supports settings limits to the area where QBasic functions can write, effectively creating a (usually) smaller "window" on the screen. The window is called a viewport and can be set using the view print statement.

     width col%, row%             # sets width (cols) & height (rows)
     view print top% to bottom%   # sets top/bottom rows

     the col%/row% values depend on the screen mode
       - col% is limited to 40 or 80 columns
       - row% is limited to 25, 30, 43, 50,or 60 rows

The view print function sets the upper/lower rows which are available for output by QBasic statements. The width statement sets the number of columns, starting with column one. Text viewports always align with the left side of the screen.

Note that when a text viewport is set, text functions may wrap to the next line (unlike graphics viewports which have no wrap equivalent).

Text Output Reference
Here's a quick reference of the available print functions, in alphabetical order.

  • color - set screen foreground, background, and border
        color fg%, bg%, border%    # syntax
        Color values:
        0   Black       8   Gray
        1   Blue        9   Light Blue
        2   Green       10  Light Green
        3   Cyan        11  Light Cyan
        4   Red         12  Light Red
        5   Magenta     13  Light Magenta
        6   Brown       14  Yellow
        7   White       15  High-intensity white

  • csrline - returns cursor row position
        result% = csrlin      # returns cursor row position

  • locate - moves cursor to specified location
        locate row%, col%, visible%, start%, stop%   # syntax
        locate 5,7         # puts cursor at row 5, column 7
    locate is used to position a cursor prior to the next print/print using/write statements. visible% = 0 for invisible cursor, visible% = 1 for visible cursor. start%/stop% define the size of the cursor (scan lines).

  • pos - returns cursor column positiont
        result% = pos(0)      # returns cursor column position

  • print - writes data to the screen using a default format
        print "hello"
        print 12.5, var%, 7

  • print using - writes data to the screen using a custom format
        print using "     "; "hello"
        print 12.5, var%, 7

    The rules for creating a format are provided at the bottom of this page.

  • screen - sets screen mode, color mode, and active/visual page. also returns ASCII value or color attribute of a character at a specified location
        screen mode%, colorswitch%, activepage%, visualpage%   # syntax
        screen 12         # sets mode to 12 (640x480)
        result = screen (row%, col%, ColorFlag%)  # syntax
        result = screen (1,1,0)     # returns ASCII value of character 1,1
        result = screen (1,1,1)     # returns color at 1,1
  • spc - moves cursor a specified number of columns
        lprint a$; spc(20); b$  # b$ prints 20 spaces after a$

  • tab - moves cursor to specified column
        lprint tab(column%), a$       # starts printing a$ in column 40

  • view print - sets boundaries of screen text viewport
        view print top% to bottom%  # syntax
        view print 5 to 10          # viewport is rows 5-10

    If you omit the toprow% and bottomrow% arguments, VIEW PRINT sets the entire screen as the text viewport. Ranges for top% and bottom% depend on the screen mode.

  • width - sets number of screen-display columns and rows
        width columns%, rows%        # syntax
        width 40, 40                 # 40 columns, 40 rows
        open "LPT1:" for output as #1
        width #1, 132                # sets printer to 132 characters wide

  • write - writes data to the screen in CSV format
        write a$, b%, c&     # writes using CSV formatting

Print Format
These characters may be used to define the format in a print using statement.

 #    Digit position.                     -     Placed after digit, prints
 .    Decimal point position.                   trailing sign for negative
 ,    Placed left of the decimal point,         numbers.
      prints a comma every third digit.   $$    Prints leading $.
 +    Position of number sign.            **    Fills leading spaces with *.
^^^^  Prints in exponential format.       **$   Combines ** and $$.

                     Characters used to format a string expression
 &    Prints entire string.               \ \   Prints first n characters,
 !    Prints only the first character           where n is the number of
      of the string.                            blanks between slashes + 2.

                     Characters used to print literal characters
 _    Prints the following formatting           Any character not in this
      character as a literal.                   table is printed as a

If you have any suggestions or corrections, please let me know.