daniel lozano
daniel lozano

Reputation: 429

how to do a newline while printing a RichTextbox in a .txt file in vb.net

I am trying to print in a .txt file the text I am printing in a RichTextBox.

The problem I am having is that in the richtextbox I am using vbCrLf each time I want to print a newline:

 Dim msg As String = selectedMarker.ToolTipText & " - Position Request: " & vbCrLf
 ShowMessageColor(msg, Color.Yellow)

Public Sub ShowMessageColor(msg_ As String, color_ As System.Drawing.Color)
    Console.ConsoleRichTextBox.SelectionStart = Console.ConsoleRichTextBox.Text.Length
    Dim oldColor = Console.ConsoleRichTextBox.SelectionColor
    Console.ConsoleRichTextBox.SelectionColor = color_
    Console.ConsoleRichTextBox.AppendText(msg_ & vbCrLf)
    Console.ConsoleRichTextBox.SelectionColor = oldColor

    Console.ConsoleRichTextBox.ScrollToCaret()
End Sub

Here I am printing a message in a specific color in the richtextbox (I am changing the color because there are different items that print message and with this I can identify who is sending me messages)

To print in the .txt file, I an using the following:

If System.IO.File.Exists(SavePathConsoleLog) Then
        log = Console.ConsoleRichTextBox.Text.ToString & vbCrLf
        File.AppendAllText(SourcePathConsoledLog, log)
    Else
        File.Create("C:\NTGS\BMS\BMS_v6\BMS_v2\ConsoleLog.txt").Dispose()
        log = Console.ConsoleRichTextBox.Text.ToString & vbCrLf
        File.WriteAllText(SourcePathConsoledLog, log)
    End If

And this is what I am obtaining:

Alakran4 - Vehicle Damages Report: Alakran4 - Position Request: Alakran4 - Vehicle and Tripulation State: AO3 - Position Request: AO3 - Target State 

All in the same line...

I don't know what I am doing wrong. Maybe can someone help me? I think that using vbCrLf, it must do it. I have used it before and I had no problems. The information in my richtextbox appears like this:

Alakran4 - Vehicle Damages Report: 

Alakran4 - Position Request: 

Alakran4 - Vehicle and Tripulation State: 

AO3 - Position Request: 

AO3 - Target State 

Thanks for any help you could give me!

Upvotes: 0

Views: 591

Answers (2)

daniel lozano
daniel lozano

Reputation: 429

Thanks for your advices!

I couldn't achieve this, so finally I decided to write the lines in the .txt file at the moment I am writing the line in the RichTextbox. Something like this:

Dim msg As String = item.ToolTipText & " - Position Request: " & vbCrLf
ShowMessageColor(msg, Color.Yellow)

Public Sub ShowMessageColor(msg_ As String, color_ As System.Drawing.Color)
    Console.ConsoleRichTextBox.SelectionStart = Console.ConsoleRichTextBox.Text.Length
    Dim oldColor = Console.ConsoleRichTextBox.SelectionColor
    Console.ConsoleRichTextBox.SelectionColor = color_
    Console.ConsoleRichTextBox.AppendText(msg_ & vbCrLf)
    timestamp = DateTime.Now
    log = timestamp & vbTab & vbTab & msg_ '& vbCrLf
    File.AppendAllText(SourcePathConsoledLog, log)
    Console.ConsoleRichTextBox.SelectionColor = oldColor

    Console.ConsoleRichTextBox.ScrollToCaret()
End Sub

With this, I write in the same function the text in the Richtextbox and in the file.

Thakns for your help! Was very useful!

Upvotes: 0

Magnus
Magnus

Reputation: 201

A RichTextBox doesn't have vbCrLf as linebreaks, it has vbLf.

To get the desired output, you should do something like:

log = Console.ConsoleRichTextBox.Text.Replace(vbLf, vbCrLf) & vbCrLf
If System.IO.File.Exists(SavePathConsoleLog) Then
    File.AppendAllText(SourcePathConsoledLog, log)
Else
    File.Create("C:\NTGS\BMS\BMS_v6\BMS_v2\ConsoleLog.txt").Dispose()
    File.WriteAllText(SourcePathConsoledLog, log)
End If

Somewhat similar to How do I retain TextBox line breaks in Winforms after assigning text to a string variable?

edit: The following example illustrates this:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "Line 1" & vbNewLine & "Line 2"
    End Sub

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        Debug.Print("Newline: " & RichTextBox1.Text.IndexOf(vbNewLine))
        Debug.Print("Cr: " & RichTextBox1.Text.IndexOf(vbCr))
        Debug.Print("Lf: " & RichTextBox1.Text.IndexOf(vbLf))
    End Sub
End Class

When run, it gived the following output:

Newline: -1
Cr: -1
Lf: 6

Upvotes: 1

Related Questions