How to save what u type in a textbox to a file?

Dim oFile as System.IO.File
Dim oRead as System.IO.StreamReader
oRead = oFile.OpenText(“C:\sample.txt)
oWrite.WriteLine(text1.text)

ur oWrite is declared as wat? as dim oWrite as System.IO.StreamWriter?

Ooops.... yes it should be.

but i still got error.
what i type
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim oFile As System.IO.File
Dim oRead As System.IO.StreamReader
Dim oWrite As System.IO.StreamWriter

oRead = oFile.OpenText("C:\Documents and Settings\NPNet\My Documents\sample.txt")
oWrite.WriteLine(TextBox1.Text)

End Sub
the error msg is

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe

Additional information: Object reference not set to an instance of an object.

Ok, As it turns out, it depends on if you are written to a new file, or appending data to an existing file..... writing data to a new file will be like this:

Dim tw As System.IO.TextWriter 
tw = System.IO.File.CreateText("C:\MyTextFile.txt") 
tw.WriteLine(text1.text)         'assuming the name of your textbox is text1
tw.Flush() 
tw.Close()

However, If you are writing to an existing file....

Dim sw As IO.TextWriter 
sw = IO.File.AppendText("C:\MyTextFile.txt") 
sw.WriteLine(text1.text)
sw.Flush() 
sw.Close()

So, You have to know if you are writing to an existing file or not, to determine how you will go about writing to it. If You want to do either, regardless if the file exists or not, you could do an if statement with File.Exists("c:\MyTextFile.txt"), which should return true or false depending on if the file exists or not.

Whew!

*Wipes His Forehead*

Thanks for ur help, it can be working. thanks a lot.

Dim sw As IO.TextWriter 
sw = IO.File.AppendText("C:\MyTextFile.txt") 
sw.WriteLine(text1.text)
sw.Flush() 
sw.Close()

Hi new to the board and new to VB. I used your code and worked great. Only difference I tryed to use it in a timer. Did not work.

I normally use

FileOpen(1, "\TestTextbox1.txt", OpenMode.Output)
        Print(1, TextBox1.Text)
        FileClose(1)

Problem with that code is it over writes the current data and text file. Your method works well with the button but is it possible to make it work in a timer without scrolling the same data over and over again? My goal with this is to have the textbox write to a textfile without user intervention.

any ideas?

Thanks

Rich

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.