PRINT

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Regular Output

Description

Causes text to be displayed in the mainwin. PRINT is also used to send data to a file or device.
For GUI programs, PRINT sends commands or data to a window or control.
(Note: The PRINT statement is a very versatile command. It has many uses. If you are just beginning with Just BASIC, you will find the first examples will show the basic uses of PRINT. More advanced uses are shown in the additional examples.)

Syntax

Note: Expressions may be literal or variable, string or numeric.

To Display Text

  1. Empty Line
    PRINT
  2. Literals
    PRINT "Text"
    PRINT 123
  3. Variables
    PRINT String$
    PRINT Number
  4. Concatenating
    PRINT String$; Number
    PRINT String$ + String$
  5. Columnar Output
    PRINT String$, Number
  6. Suppressing New Line
    PRINT String$;
    PRINT Number;
    PRINT String$; Number;

To Write to a File

Sequential files (opened for APPEND or for OUTPUT):
  1. Empty Line
    PRINT #handle, ""
  2. Line of data
    PRINT #handle, one or more expressions separated by semi-colons (;)
  3. Printing data, suppressing new line
    PRINT #handle, one or more expressions ending with a semi-colon (;)
Binary files (opened for BINARY):
Since printing to binary files does not append new line, (1) and (3) does not apply.

Use with GUI

Long form (outdated):

  1. PRINT #handle, {command}
  2. PRINT #handle, {text string} or {literal number}
  3. PRINT #handle, {var$} or {numVar}

Short form (preferred form): Omits the word "Print" and the comma after #handle.

  1. #handle {command}
  2. #handle {text string} or {literal number}
  3. #handle {var$} or {numVar}

Hints

Place useful hints about this keyword here

Example

Regular Output

'Printing to the mainwin
print "A printing test"
print ""                        'prints a blank line with line feed
print "hello world"             'prints content and LF, CR
print "hello world";            'semi-colon (;) suppresses line feed
                                'next printed item will be on same line
print " and this on same line."
end
'using PRINT for columnar output
print "C-1", "C-2"              'A comma at the end of an expression will
                                'cause output to be printed in columns.
print "C-1",,,"C-4"             'Extra commas bypass that column.
end

Printing to Disk Files

    open "addData.txt" for output as #myfile

    print #myfile, "Data One"     'data will be stored on disk as one item on one line
    print #myfile, "Data Two";    'semi-colon causes next data item sent to the file
                                  ' to be stored on the same line with no space between
    print #myfile, "Data Three"

'To separate the data, and provide a way to read data back as individual items,
'  print a comma (,) between the items.
    print #myfile, "Data Four"; ","; "Last Data"

    close #myfile
    end
' view file in Notepad, or print contents to see format

GUI Output

' In the GUI environment, printing content or commands to a window or control is also done
'  with the PRINT command. To achieve this, the handle of the window or control must be
'  included in the statement.

' The following examples show the syntax of a print statement for this use and are not
'  complete, working programs.

    print #main, "TABLE ONE"                 'Printing literal text to a window will work
                                             ' only in windows of type "text".

' to print to controls, use the window's handle and the control's extent name
    print #main.statictext1, "Item One"      'prints to a statictext control
                                             ' with the extent "statictext1"

' Printing a command to a window or control
    print #main "trapclose [quit]"           'prints to the window
    print #main.textbox1, ""                 '"commands" the textbox to clear it's contents

'Note: See other BB pages for definitions of controls and commands.

Useful Procedures

' Truncating data in columns in the mainwin
' Often, a "~" character is used to indicate the data has been truncated
name$(1) = "Jackson Brown"
name$(2) = "Elizabeth Warren"
name$(3) = "Gomer Pyle"

for i = 1 to 3
    print displayName$(name$(i)), "000";i
next i

function displayName$(n$)
    if len(n$) > 12 then
        displayName$ = left$(n$,11) ; "~"
    else
        displayName$ = n$
    end if
end function
end
' Output:
'   Jackson Bro~  0001
'   Elizabeth W~  0002
'   Gomer Pyle    0003

' Caveats:
'    Columnar data in the mainwin works best when mono-spaced font is used.
'    Columns produced by using the comma are about 14 spaces wide; some trial
'    and error may be required for the number of characters to return with the 
'    left$() function.

Graphics Method

Description

This command sends the plotted image to the Windows Print Manager for output. The size argument is optional and is not dependent upon the user's display resolution (See table below).

Syntax

  1. PRINT #handle, "print"
  2. PRINT #handle, "print 420"
  3. PRINT #handle, "print vga"
  4. PRINT #handle, "print svga"
  5. PRINT #handle, "print xga"
  6. #handle "print"
  7. #handle "print 420"
  8. #handle "print vga"
  9. #handle "print svga"
  10. #handle "print xga"

Hints

Make sure to have only one FLUSH issued before you print or some of the contents will not be printed.
If you need more FLUSH commands, use the following approach to get a full printout:
  1. GETBMP the contents of the graphics control or window
  2. CLS
  3. DRAWBMP the previously captured image
  4. FLUSH
  5. PRINT
Scaling to full Paper Width
Size Pixels
any number as specified
vga 640
svga 800
xga 1024

Example

nomainwin

' Open a graphics window
    open "Draw a Circle" for graphics as #g
    #g "trapclose [quit]"    ' trap close event

' position pen in center of window and drop pen down
    #g "home; down"

' draw a circle with radius of 50 pixels
    #g "circle 50"

    #g "up"           ' raise the pen

' go to location under the circle to print label
    #g "goto 125 225"

    #g "down"         ' put pen down

' write the label "Circle"
    #g "\Circle"

    #g "flush"        ' make the graphics "stick"

    #g "print"        ' Send drawing to printer

    wait              ' wait for a user event

' event handler for closing window
[quit]
    close #g          'close the graphics window
    end

Useful Procedures

' Place a useful function or sub using this keyword here