Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write textbox values to .txt file with vb.net

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.

like image 990
jason Avatar asked Feb 15 '11 10:02

jason


People also ask

How do I write to a text file in VB?

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.

How do you use text boxes in Visual Basic?

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.

How get TextBox value from one form to another in VB net?

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.


2 Answers

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()
like image 199
hawbsl Avatar answered Sep 22 '22 13:09

hawbsl


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
like image 31
Gabriel Spiteri Avatar answered Sep 21 '22 13:09

Gabriel Spiteri