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
2009-08-19 18:50:37
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
2009-08-19 18:51:36
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
2009-08-19 19:04:56
+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
2009-08-19 19:22:12
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
2009-08-19 20:26:56
By the way, ControlChars.Lf is equivalent to Chr(10), as ezwi and I mentioned earlier :)
Anders
2009-08-19 20:29:27
Again, however, this is a VB-specific function. Why not use Environment.Newline so that it's language agnostic?
Adam Robinson
2009-08-20 14:34:36
Actually had tried environment.newline with no success. thanks for the suggestion though.
Scott Boettger
2009-10-13 14:34:19