Try this:
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllText(nsavetxtoutput.FileName, TextBox2.Text)
End If
Where TextBox2
is your output TextBox.
More information at MSDN Documentation.
Question
Good morning everyone, I'm working on my finals project in visual basic and i want to implement a save file option.
Right now the application has 2 textboxes (not richtextbox) which are the input and output of information.
What i want to do is save only the content of the output textbox. I managed to get it to save something to file but when its opened it always turns out to be empty.
the code example of the save file button is below, i have a feeling its not saving the content because it is not specified, yet i have no idea how to specify to only save the content of the one textbox even with the many hours of forum / google searching ive done to try and figure out on my own.
Dim myStream As Stream
Dim nsavetxtoutput As New SaveFileDialog()
'|All files (*.*)|*.*
nsavetxtoutput.Filter = "txt files (*.txt)|*.text"
nsavetxtoutput.FilterIndex = 2
nsavetxtoutput.RestoreDirectory = True
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
myStream = nsavetxtoutput.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
Any and all insight would be much appreciated!
Thank you guys!
The Program allowed to save only the contents of the textbox with this function - Thanks very much everyone who replied. it helped out allot!
Private Sub NOTEPAD_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave2Notepad.Click
Dim nsavetxtoutput As New SaveFileDialog()
nsavetxtoutput.Filter = "txt files (*.txt)|*.text"
nsavetxtoutput.FilterIndex = 2
nsavetxtoutput.RestoreDirectory = True
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllText(nsavetxtoutput.FileName, txtoutput.Text)
End If
End Sub
La solution
Try this:
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllText(nsavetxtoutput.FileName, TextBox2.Text)
End If
Where TextBox2
is your output TextBox.
More information at MSDN Documentation.
Autres conseils
You can write to a file using File.WriteAllText
method. It takes two parameters. The first is the path to the file, which you get from the SaveFileDialog
. The second is the value you want to write to the file.