Question How to make a Log .txt file from TextBox

BlueEyed

New member
Joined
Mar 2, 2011
Messages
3
Programming Experience
Beginner
Hi everybody, thanks for reading, im a beginner with VB .NET and i was wondering how i can make VB to write the data from my textbox to a .txt , making it a log. The thing is my textbox is multiline, but when i log the data to the .txt it perfectly works BUT it doesnt do it in a multi line way. For example this is MY textbox:

[Date/Hour] You have entered the matrix
[Date/Hour] Thanks for entering the matrix.

and this is how it logs to the .TXT:

[Date/Hour] You have entered the matrix [Date/Hour] Thanks for entering the matrix.

This is the way i use it :

VB.NET:
  Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        RichTextBox1.Text = RichTextBox1.Text & vbNewLine
        Dim FILE_NAME As String = "C:\log.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            objWriter.Write(RichTextBox1.Text & Environment.NewLine)

            objWriter.Close()
            MsgBox("Text written to file")
        Else
            MsgBox("File Does Not Exist")
        End If

        Me.Close()
    End Sub

How can i make my .txt log to be as my textbox, multiline, thanks in advance and for answering my question.
 
The RichTextBox control uses its own linefeed (vbCr only?) no matter what you do, while Notepad only reads vbCrLf/VbNewline as linefeeds. If you open it in WordPad you will see the linefeeds properly.
For 'full' linefeeds you could use the SaveFile method specifying plain text format. Or you could WriteLine each line in the Lines array. Or do a String.Join on the Lines array and output that.
 
Thanks for answering, im gonna leave it like that, i ll use WordPad for reading the text, now how can i make it go to the last line of the .TXT? i mean, if i use that code, everytime the program writes to the .TXT it deletes all the old data stored in it, how can i make it go to the last line and keep adding data without erasing the old data? thanks in advance!
 
Back
Top