I'm trying to make a button called "Save" which saves all of the information entered into the textboxes as a .txt document. I'm very notice in this language and I don't know how to do it.
IO.File.WriteAllText
^ That's what I have now.
Printable View
I'm trying to make a button called "Save" which saves all of the information entered into the textboxes as a .txt document. I'm very notice in this language and I don't know how to do it.
IO.File.WriteAllText
^ That's what I have now.
What exactly do you not understand about the examples of File.WriteAllText that you found on the web? I just Googled and the first result was the MSDN documentation, which is what everyone should read first anyway.
http://msdn.microsoft.com/en-us/library/ms143375.aspx
That includes, amongst other information, a code example. Is there anything in that you're not clear about?
try this:
vb Code:
Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 'save IO.File.WriteAllLines("c:\example.txt", New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text}) End Sub Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click 'open Dim lines() As String = IO.File.ReadAllLines("c:\example.txt") If lines.Length <> 3 Then Return TextBox1.Text = lines(0) TextBox2.Text = lines(1) TextBox3.Text = lines(2) End Sub End Class
Ah, I missed that it was TextBoxes rather than TextBox. My apologies.
The code works sort've. What would I put for c:\example.txt If I just want it to open up the save dialog box as a .txt document on anyones computer.
vb Code:
Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 'save Dim sfd As New SaveFileDialog With { _ .Title = "Choose file to save to", _ .Filter = "TXT (*.txt)|*.txt|All Files (*.*)|*.*", _ .FilterIndex = 0} if sfd.showdialog = dialogresult.ok then IO.File.WriteAllLines(sfd.filename, New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text}) end if End Sub Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click 'open Dim ofd As New OpenFileDialog With { _ .Title = "Choose file to open", _ .Filter = "TXT (*.txt)|*.txt|All Files (*.*)|*.*", _ .FilterIndex = 0} if ofd.showdialog = dialogresult.ok then Dim lines() As String = IO.File.ReadAllLines(ofd.filename) If lines.Length <> 3 Then Return TextBox1.Text = lines(0) TextBox2.Text = lines(1) TextBox3.Text = lines(2) end if End Sub End Class
Still have no clue what I'm doing, can anyone give me some insight?
post #6 is the working code.