I have a simple form with two textboxes, I want Textbox1
to write to a file named C:\VALUE1.txt
and Textbox2
to write its value to a file named C:\VALUE2.txt
Any text that is already in the text file MUST be over written.
Use the WriteAllText method to write text to a file, specifying the file and text to be written. This example writes the line "This is new text." to the file named test. txt , appending the text to any existing text in the file.
Text box controls allow entering text on a form at runtime. By default, it takes a single line of text, however, you can make it accept multiple texts and even add scroll bars to it. Let's create a text box by dragging a Text Box control from the Toolbox and dropping it on the form.
In order to retrieve a control's value (e.g. TextBox. Text ) from another form, the best way is to create a module and create a property for the private variable. Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text.
It's worth being familiar with both methods:
1) In VB.Net you have the quick and easy My.Computer.FileSystem.WriteAllText option:
My.Computer.FileSystem.WriteAllText("c:\value1.txt", TextBox1.Text, False)
2) Or else you can go the "long" way round and use the StreamWriter object. Create one as follows - set false in the constructor tells it you don't want to append:
Dim objWriter As New System.IO.StreamWriter("c:\value1.txt", False)
then write text to the file as follows:
objWriter.WriteLine(Textbox1.Text)
objWriter.Close()
Dim FILE_NAME As String = "C:\VALUE2.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(TextBox2.Text)
objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With