Save Text Box Data

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I was wondering how I could have a option in the File menu to save whats in a multiline enabled textbox that I have on one of my forms.

Im kinda a beginner to vb.net so if you could, please explain what I need to do with any code you post.

Form Info:
Text box name- TextBox1
Form name- frmNotepad

Thanks alot in advance
 
a .txt file in a specified path.

Path would be:

Application.StartupPath & "\SideKick Files\"
 
You can use the StreamWriter class

[VB]
Dim FileWriter As New StreamWriter("PathOfYourFile")
FileWriter.WriteLine(TextBox1.Text)
[/VB]

Hope this helps,
 
And will that then remember it and open it in the textbox (multiline) next time you open the form? If not, whats the code to do that?
 
in your Forms Load event , you could check if the file exists or not , then load it if it does , eg:
Code:
/// as Mehyar said You can use the StreamWriter class if you want to write the file.
///
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String = "C:\some path.txt"
        If IO.File.Exists(path) Then
            Dim sReader As New IO.StreamReader(New IO.FileStream(path, IO.FileMode.Open))
            While Not sReader.Peek = -1
                TextBox1.AppendText(sReader.ReadLine)
            End While
        Else
            /// the file doesnt exist yet!
        End If
    End Sub
 
nope.. when i click the Save in the File menu, it has an error and is unable to save the text to a file.
 
ok, i was kinda able to get it to work a little, I entered the path as Application.StartupPath & "\Sidekick Files\Notepad.txt", but when I save it, it creates the file, but it doesnt save anything in it, its blank.
 
Heres one of my functions to save line from a listbox...

Code:
Function WriteKeepersToFile()
        Dim ListCount As Integer = lstKeepers.Items.Count
        Dim fw As StreamWriter
        Dim j As Int16
        Dim ReadString As String
        Try
            Pass the file path and name to the StreamWriter constructor.
           
            fw = New StreamWriter("Keepers.mwm") This saves the file in the program directory.  While devoloping, it will be the Bin folder


            For j = 0 To ListCount - 1Loop thru alll items in the listBox
                ReadString = lstKeepers.Items(j) read one string at a time
                fw.WriteLine(ReadString)write one string at a time
            Next j

        Catch
            MsgBox("Error saving numbers to ""keepers.mwm"" file.", MsgBoxStyle.Critical, ProgramName)
        Finally
            Close the file.
            fw.Close()
        End Try

End Function

Works for me. Im new at .NET also....... Takes a lot of reading
 
Back
Top