[c#] join lines in richtextbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • booyeeka
    New Member
    • Mar 2009
    • 4

    [c#] join lines in richtextbox

    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(Environ ment.NewLine, "")........ :-((

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

    please help!!!
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I bet I know what you did :D

    You likely went...

    Code:
    myRichTextBox.Rtf.Replace("\\par", "");
    You actually want to do...

    Code:
    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...?
    Last edited by GaryTexmo; Oct 28 '09, 02:29 PM. Reason: Update for accuracy

    Comment

    • booyeeka
      New Member
      • Mar 2009
      • 4

      #3
      nope
      :-(

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

      myRichTextBox.R tf = myRichTextBox.R tf.Replace("\\p ar", "");

      i tryed with
      \\pard
      \\pad
      \\n
      \\r\\n
      Enviroment.Newl ine

      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?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        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...

        Code:
        string modifyRtf = myRichTextBox.Rtf;
        modifyRtf = modifyRtf.Replace("\\pard", "");
        modifyRtf = modifyRtf.Replace("\\par", "");
        myRichTextBox.Rtf = modifyRtf;
        The way I went about this was to do a Console.WriteLi ne on myRichTextBox.R tf 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...


        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.

        Comment

        Working...