Text Window Commands

The commands in this topic work with the text window and texteditor control. Anything printed to a text window is displayed exactly as sent. To distinguish commands sent to a text window from text that is to be displayed in the window, the ! character should be the first character in the string. It is no longer necessary to add a semicolon to the end of a printed command line to suppress a carriage return. The semicolon at the end of a printed command is now optional. When printing text, rather than commands, a carriage return is added to the text with each print statement, unless the statement ends with a semicolon.

Note: for instructions on sending text to the printer, see LPRINT.

For more detailed instructions on text string functions, see the section on Text Usage.

Using variables in text commands:

Literal values are placed inside the quotation marks:

  print #handle, "!line 3 string$"

Variables must be places outside the quotation marks, with blank spaces preserved:

  lineNum=3
  print #handle, "!line ";lineNum;" string$"

See also: Understanding Syntax - how to use literals and variables in commands.

For example:

  'open a text window
  open "Example" for text as #1

  'print Hello World in the window
  print #1, "Hello World"

  'change the text window's font
  print #1, "!font helv 16 37"

  'read line 1
  print #1, "!line 1"
  input #1, string$
  print "The first line is:"
  print string$
  input "Press 'Return'"; r$

  'close the window
  close #1

Proper use of semicolons and the 'print' command.

In the following example, semicolons are omitted in printed commands. They are used to force carriage returns when printing text. It is also possible to omit the word "print" and to omit the comma after the handle when printing to a text window or texteditor. This means that the word "print" and the comma following the handle are optional.

  nomainwin
  open "Example" for text as #1
  print #1, "!trapclose [quit]"
  'carriage return suppressed by semicolon:
  print #1, "Hello";
  'this line will have a carriage return:
  #1 " World"
  'this command omits the word 'print'
  #1 "!font courier_new 12"
  'print some more text into the window, no carriage return:
  print #1, "This is a font called ";
  'print text with carriage returns:
  print #1, "courier_new."
  'blank line:
  print #1, ""
  #1 "Done!"
  wait
[quit]
  close #1
  end

Note: Most of the commands listed below work with windows of type "text" and also with the "texteditor" control except where noted.

Here are the text window commands:

print #handle, "!backcolor color";
print #handle, "!forecolor color";

These commands set the background and foreground colors of all text in the control or window.

  open "color example" for text as #t
  #t "!backcolor black";
  #t "!forecolor green";
  #t "Liberty BASIC v4.5.0"
  wait

Here is a list of valid colors (in alphabetical order):

black, blue, brown, buttonface, cyan, darkblue, darkcyan, darkgray, darkgreen, darkpink, darkred, green, lightgray, palegray, pink, red, white, yellow

Palegray and Lightgray are different names for the same color. Buttonface is the default background color currently set on a user's system, so it will vary according to the desktop color scheme. Here is a graphical representation of the named colors:

print #handle, "!cls";

This command clears the text window of all text.

print #handle, "!contents varname$";
print #handle, "!contents #handle";

This command has two forms as described above. The first form causes the contents of the text window to be replaced with the contents of varname$, and the second form causes the contents of the text window to be replaced with the contents of the stream referenced by #handle. This second form is useful for reading large text files quickly into the window.

Here is an example of the second form:

  open "Contents of AUTOEXEC.BAT" for text as #aetext
  open "C:\AUTOEXEC.BAT" for input as #autoexec
  print #aetext, "!contents #autoexec";
  close #autoexec
  'stop here
  input a$

print #handle, "!contents? string$";

The !contents? command returns the entire text of the window. After this command is issued, the entire text is contained in the variable string$.

print #handle, "!copy";

The !copy command causes the currently selected text to be copied to the WINDOWS clipboard.

print #handle, "!cut";

The !cut command causes the currently selected text to be cut out of the text window and copied to the WINDOWS clipboard.

print #handle, "!cut";

The !cut command causes the currently selected text to be cut out of the text window and copied to the WINDOWS clipboard.

Note: See the example program find example.bas for a working demo of how to use !find, !findback and !resetfind.

print #handle, "!find findThis$";

The !find command searches forward from the beginning of the text for a string specified in findThis$.  If it finds it then it will select that string and scroll the view to make that selection visible.  Successive uses of !find will find each next match until no more matches are found.

print #handle, "!findback findThis$";

The !findback command searches backwards from the end of the text for a string specified in findThis$.  If it finds it then it will select that string and scroll the view to make that selection visible.  Successive uses of !find will find each next match until no more matches are found.

print #handle, "!resetfind";

The !resetfind command resets the search mechanism so the next FIND or FINDBACK will search from the start or end of the text respectively.

print #handle, "!font fontName pointsize" ;

The !font command sets the font of the text window to the specified name and size. If an exact match cannot be found, then Liberty BASIC will try to match as closely as possible, with size taking precedence over name in the match. Note that a font with more than one word in its name is specified by joining each word with an underscore _ character. For example, the font Times New Roman becomes Times_New_Roman, and the font Courier New becomes Courier_New.

Example:

  print #handle, "!font Times_New_Roman 10";

For more on specifying fonts read How to Specify Fonts

print #handle, "!insert varname$";

The !insert command inserts the contents of the variable at the current caret (text cursor) position, leaving the selection highlighted.

print #handle, "!line n string$";

The !line command returns the text at line n. In the code above, n is standing in for a literal number. If n is less than 1 or greater than the number of lines the text window contains, then "" (an empty string) is returned. After this command is issued, the line's text is contained in the variable string$.

This sample code retrieves the contents of line number 7 and places them into a variable called string$:

  print #textwindow, "!line 7 string$"

print #h, "!lines countVar";

The !lines command returns the number of lines in the text window, placing the value into the variable countVar.

print #handle, "!modified? answer$";

The !modified? command returns a string (either "true" or "false") that indicates whether any data in the text window has been modified. The variable answer$ holds this returned string. This is useful for checking to see whether to save the contents of the window before closing it.

print #h, "!origin? columnVar rowVar";

The !origin? command causes the current text window origin to be returned. The origin is the upper left corner of the texteditor or textwindow. When a text window is first opened, the result would be row 1, column 1. The result is contained in the variables rowVar and columnVar.

print #handle, "!origin column row";

The !origin command forces the origin of the window to be row and column. This means that the row and column specified will appear in the upper left corner of the texteditor or text window. Row and column must be literal numbers. To use variables for these values, place them outside the quotation marks, preserving the blank spaces, like this:

  print #handle, "!origin ";column;" ";row

print #handle, "!paste";

The !paste command causes the text in the WINDOWS clipboard (if there is any) to be pasted into the text window at the current cursor position.

print #handle, "!select column row" ;

The !select command puts the blinking cursor at column row. Column and row must be literal numbers. To express them as variables, place the variables outside the quotation marks and preserve the blank spaces, like this:

  print #handle, "!select ";column;" ";row

print #handle, "!selectall";

The !selectall command causes everything in the text window to be selected (highlighted).

print #handle, "!selection? selected$";

The !selection? command returns the highlighted text from the window. The result will be contained in the variable selected$.

print #handle, "!setfocus";

The !setfocus command causes Windows to give input focus to this control. This means that, if some other control in the same window was highlighted and active, this control now becomes the highlighted and active control, receiving keyboard input.

print #handle, "!trapclose branchLabel";

The !trapclose command tells Liberty BASIC to continue execution of the program at branchLabel if the user double clicks on the system menu box or pulls down the system menu and selects "close."