Connecting Tech Pros Worldwide Help | Site Map

[c#] join lines in richtextbox

Newbie
 
Join Date: Mar 2009
Posts: 4
#1: 3 Weeks Ago
on a windows form, i have RichTextBox, with some text, in several lines. and one button on a form.

i wolud like when i click on that button, to join all richtextbox of lines in one line, but not to loose text style (like font family, color, etc.)

i can not do it with Replace, like \r\n, and not with the Replace(Environment.NewLine, "")........ :-((

i have also tryed to replace \par and \pard, but still no luck.......

please help!!!
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: 3 Weeks Ago

re: [c#] join lines in richtextbox


I bet I know what you did :D

You likely went...

Expand|Select|Wrap|Line Numbers
  1. myRichTextBox.Rtf.Replace("\\par", "");
You actually want to do...

Expand|Select|Wrap|Line Numbers
  1. myRichTextBox.Rtf = myRichTextBox.Rtf.Replace("\\par", "");
The string replace method doesn't modify the actual string, it returns the modified string instead, so you need to assign it. I gave this a try and it worked for me, though there was some unexpected side effects. I'm not sure if you should replace the par... I don't really know the rich text format offhand... let me play around a bit more.

** Update:
Ok, it looks like the "\par" code is the newline, but there's also a "\pard" code in there. Since the string.Replace method isn't very choosy, it will knock out a "\pard" code and make it a "\d" code, which was generating my issue.

Knowing nothing about the Rtf format, I blindly replaced "\pard" with blank before I did "\par" and it worked, but I'd highly recommend making a better replace function on your own to make sure you knock out exactly what you want.

Maybe someone else has a suggestion for a "safer" way of doing this...?
Newbie
 
Join Date: Mar 2009
Posts: 4
#3: 3 Weeks Ago

re: [c#] join lines in richtextbox


nope
:-(

i had sevaral attempts to solve this... all of them was with code like

myRichTextBox.Rtf = myRichTextBox.Rtf.Replace("\\par", "");

i tryed with
\\pard
\\pad
\\n
\\r\\n
Enviroment.Newline

etc etc, but i can not get result i want...

i simply can not understand where is the problem... is maybe something to do with character encoding?
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#4: 3 Weeks Ago

re: [c#] join lines in richtextbox


Well, my final solution replaced \pard with nothing, then \par with nothing. I'm guessing the control was smart enough to figure out when something was missing because when I checked the text after the replacement, the \pard was back but the \par wasn't.

Oh! I forgot to mention, since the richtextbox sorts itself out when there are errors, you have to do the change as such...

Expand|Select|Wrap|Line Numbers
  1. string modifyRtf = myRichTextBox.Rtf;
  2. modifyRtf = modifyRtf.Replace("\\pard", "");
  3. modifyRtf = modifyRtf.Replace("\\par", "");
  4. myRichTextBox.Rtf = modifyRtf;
The way I went about this was to do a Console.WriteLine on myRichTextBox.Rtf and see what was there. Then I did my replacements and outputted the Rtf again to compare. That's how I saw the issue with \pard.

Maybe doing that kind of comparison would help you work through it?

**** IMPORTANT ****
Here's the specification for the RTF format...
http://www.microsoft.com/downloads/d...displaylang=en

On page 144, line 32 it mentions that \par is indeed what a carriage return/line feed is. However, \pard is something entirely different, so you'll definitely want to make your own replacement method so you don't blow away \pard when replacing \par with nothing.
Reply