views:

871

answers:

1

I am using the System.Windows.Forms.RichTextBox control in .NET 2.0 and am using c#.

I have an array of strings that I would like to append to the RichTextBox, but I do not want to create new paragraphs in the RTF. I want the lines to be divided by \line commands, not \par commands in the RTF. (I want to simulate someone typing shift+enter instead of just enter.)

I have not been able to successfully get RichTextBox.AppendText to generate a \line command instead of a \par command. So I have been inserting my text and then manually inserting the \line command into the RTF itself.

This fails when one of the strings contains characters that are escaped in the RTF (a single closequote is converted to \rquote for example) because I am not able to find the original string I inserted into the RTF.

With this solution, I will need to escape out the characters in my line before searching for it in the RTF.

Is there a way to get the RichTextBox control to generate the \line command and not the \par command or will I have to stick with this solution and search for the RTF escaped string.

Here is my current code:

string[] lines = textInfo.Lines;

for (int i = 0; i < lines.Length; i++)
{
    rtb.AppendText(lines[i]);

    if (i < lines.Length - 1)
    {
        string rtf = rtb.Rtf;

        int insertedIndex = rtf.LastIndexOf(lines[i]);

        int length = lines[i].Length;

        rtf = rtf.Insert(insertedIndex + length, @"\line");

        rtb.Rtf = rtf;
    }
}
A: 

The RichTextBox control automatically updates the Text and Rtf properties when you modify either of them. Any text appended to the Text property will automatically be updated to the RTF property.

From some quick testing, it seems the RichTextBox control automatically inserts \par to any newline (enter, shift-enter on the control and \r \n \r\n Environment.NewLine on the Text property and the AppendText() function on the control itself). Which is what you report as your problem.

I don't believe there is any way to change how the control generates RTF. I would just append the text and let the RichTextBox control handle the conversion of any characters that may need escaping and then replace all the \par with \line.

Assuming textInfo is a TextBox and you want to append its contents to the end of some already existing RTF (if not replace += for = in line 1

rtb.Text += textInfo.Text;

rtb.Rtf = rtb.Rtf.Replace(@"\par", @"\line");

Edit:

Since you're appending lines to your document line by line, and you know the control generates a \par for \r\n, you could postfix some unique text(you can get creative here, I used REPLN) to the lines you want to break with a \line. Of course, you'd have to be able to identify the lines you want to be replaced, which I assume you do from your question. After appending your unique text, you can then replace again REPLN\par with \line. Not as elegant as I would have liked, but it gets the job done.

foreach (string line in textInfo.Lines)
{
   //assuming your condition is that the string contains the substring, "substring"
   rtb.AppendText(line + (line.Contains("substring") ? "REPLN\r\n" : "\r\n"));
}

rtb.Rtf = richTextBox1.Rtf.Replace(@"REPLN\par", @"\line");
Luis
That was my finding too. I don't want to replace all \par instances with \line instances in the RTF, so I think I will have to continue to use my solution unless I find something better.
jameswelle
I guess you're right, that would kill any paragraphs in your document.Unless you find a way to selectively change the way the RichTextBox control does line breaks, it's hack time! Editing the answer...
Luis