Reading and writing text

By | 2018-02-04

These two snippets of code demonstrate how to read a file, as well as how to save a file.

For both of these, the code is shown as it would appear in the click event of a button. However, you can trigger this with anything, such as a timer or a condition changing state.

In the first snippet, I am demonstrating how to save the content of a text box to a text file. You can something similar to save the contents of an array or string variable.

Private Sub Command1_Click()
    Filename = InputBox$("Enter A Filename To Save The text")
    Open Filename for Output As #1
    Print #1, Text1.text
    Close #1

End Sub

For the second code snippet, I am showing how to load the content of a text file in to a text box. Similar to the output method above, the target for the text file data could also be a string variable. With slightly different code, you could also load the file into an array.

Private Sub Command2_Click()
    Filename = InputBox$("Enter The Filename To Open")
    Open Filename for Input As #1
    Text1.text = Input(LOF(1), 1)
    Close #1

End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.