Dumping a file directly into a texteditor

This month's tip is about loading text files directly
into a texteditor window.  The functionality was documented
in earlier versions of LB, but often missed.  The really
great thing is the timesavings that this little technique 
can save, in programming as well as execution times.

Here is how it works:
Open a window in your code which has a texteditor placed
on it.  Initially the texteditor will be blank.  filling 
it with text is a matter of three simple steps (and the 
first one can be skipped if you already know the name of
the file you plan to load).

First get the name of the file to be loaded into the 
texteditor.  Unless you already know the name of the file,
simply open a filedialog window and let the user pick a
file.  Here are the commands to do that part:

    fileName$ = ""
    FileDialog "Open text file", "*.txt", fileName$

Now we should have a valid filename from the operating 
system.  It is possible that the user pressed cancel, 
so we will test the string to insure it contains a value.
If it does we will open the file.  Here is the code:

    If fileName$ <> "" Then
      Open fileName$ For Input As #file

Continuing in the same IF-THEN block, we will simply 
"print" the contents of the file into the texteditor.
This is done by using the "!contents" text command.  The
help file says the following about the command:

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

This 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.

So, we should be able to load the entire file we just 
opened (as #file) into the texteditor with the following
command:

      #main.tedit, "!contents #file";

Don't forget to close the file after you are done with it,
and end the IF-THEN block.

      Close #file
    End If

So, that is how it is done.  There is a short demo program
that accompanies this newsletter called LBNR.BAS from
which this code snippet came from (it is included in a zip
file - LBNR.ZIP which is attached).  You can see the code
in action if you load and run the program in LB3.x or
higher.


