views:

1354

answers:

5

I am using a Rich TextBox in VB.NET and passing to a StringBuilder. I need to replace the New Line character from the TextBox with either a space or a comma. Problem is the standard codes for new line don't seem to be picking up this New Line character in this case. Is there a specific character used in Rich TextBoxes as the New Line? Any help or suggestions will be appreciated.

+3  A: 

You can try Environment.NewLine as a language-agnostic constant for a true newline (you should try to avoid language-specific constants and functions when possible).

RichTextBox1.Text = RichTextBox1.Text.Replace(Environment.NewLine, ",")
Adam Robinson
A: 

Sometimes it is just chr(10) or chr(13). VbCrLf is a combination of both chr(10) and chr(13).

Try parsing out one or the other, and see if that helps.

Anders
A: 

You could use the RichTextBox's Lines property. You could then insert your commas or however you're delimiting it as follows

Dim sb As New StringBuilder(String.Join(",", Me.richTextBox1.Lines))
JHBlues76
A: 
Dim linesJoined As String = String.Join(",", Me.RichTextBox1.Lines)
Dan Tao
+2  A: 

I was able to figure it out. You have to use ControlChars.Lf so the code would be along the lines of RichTextBox1.Text = RichTextBox1.Text.Replace(ControlChars.Lf, ",")

Scott Boettger
Ah yes, ControlCharacters. I completely forgot about those since I have been working almost exclusively in C# for a good while now. Glad you solved it!
Anders
By the way, ControlChars.Lf is equivalent to Chr(10), as ezwi and I mentioned earlier :)
Anders
Again, however, this is a VB-specific function. Why not use Environment.Newline so that it's language agnostic?
Adam Robinson
Actually had tried environment.newline with no success. thanks for the suggestion though.
Scott Boettger