Hey guys welcome to another visual basic tutorial last lesson what happened was
we made a program that allows us to read from a text file so I have a text file
with the contents Eddie 2500 Phil 360 and I click read from file and it loads the
contents of that text file which was called score.txt and puts it in the
program so make sure you have got this far from the last tutorial because this
tutorial is a continuation from that tutorial okay so what we're going to do
in this tutorial is learn how to write into the text file so the first thing
we're going to do is make some changes to the interface instead of a label I'm
now going to put a textbox onto the screen so delete label one and I'm going
to drag a text box onto the screen and this the name of the text box is text
box one and I want to change the properties so that the multi-line
property is changed to true and once I have done that you will find that you
can drag the text box to make it bigger now the read from file button is still
the first button is going to say write to file so click on that button and
change it to write to file by changing the text property of button 2 so the
display is now write to file the third button we're going to make is
called clear and what this button does is it's going
to clear the text box okay so let's just check whether read from file works so
double click on the button read from file and notice how it's highlighted
Label1 that's because we have just deleted Label1 and what we want to do
is change label 1 to text box 1 because that is the name of our text box so text
box 1.text equals temp s which is the string that contains the contents of
that file so run it and if you click read from file it works but these two
buttons haven't been programmed yet now the easiest button to program is clear
so let's do that first so go back to your program double click
on clear and what clear is going to do is simply clear the text box so I'm
going to change the coding for button 3 click to text box 1 dot clear and
brackets so basically all this is going to do is clear text box 1 and if you run
it and you read from the file you clear it alright so the buttons are now gone
okay so now let's program button 2 which is write to file now it's going to
have a similar structure to button 1 and the first thing we want to do is
declare a variable called file num and use the free file function to find a
number that is not taken as the file number okay the next thing you want to
do is open up the file score.txt and this time the open mode instead of input
is going to be output and the reason is because we're outputting the contents of
our program into the file okay the third thing we're going to do is use
the print line function so print line and what this function does is it
saves the string to our file so the first argument is the file number and
the second argument is the string which is coming from the text box one
dot txt so everything in text box one inside text box one is going to be saved
into the file and then we're going to close the file
okay now let's check if this works by running the program and so let's read
from the file clear read from the file now I'm going
to add another two lines let's call it John 500 write to the file clear it if
you read from the file again it's going to read the contents of that file which
is now containing John 500 and if you actually go to the folder where score.
txt is saved ah which you should remember from last
lesson if you double-click on that txt file it certainly does have John 500 in
it okay so thanks for watching the tutorial
on how to write to a text file from Visual Basic see you again next time.