Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append \line into RTF using RichTextBox control

Tags:

c#

richtextbox

When using the Microsoft RichTextBox control it is possible to add new lines like this...

richtextbox.AppendText(System.Environment.NewLine); // appends \r\n

However, if you now view the generated rtf the \r\n characters are converted to \par not \line

How do I insert a \line control code into the generated RTF?

What does't work:

Token Replacement

Hacks like inserting a token at the end of the string and then replacing it after the fact, so something like this:

string text = "my text";
text = text.Replace("||" "|"); // replace any '|' chars with a double '||' so they aren't confused in the output.
text = text.Replace("\r\n", "_|0|_"); // replace \r\n with a placeholder of |0|

richtextbox.AppendText(text);

string rtf = richtextbox.Rtf;
rtf.Replace("_|0|_", "\\line"); // replace placeholder with \line
rtf.Replace("||", "|"); // set back any || chars to |

This almost worked, it breaks down if you have to support right to left text as the right to left control sequence always ends up in the middle of the placeholder.

Sending Key Messages

public void AppendNewLine()
{
    Keys[] keys = new Keys[] {Keys.Shift, Keys.Return};
    SendKeys(keys);
}

private void SendKeys(Keys[] keys)
{
    foreach(Keys key in keys)
    {
        SendKeyDown(key);
    }
}

private void SendKeyDown(Keys key)
{
    user32.SendMessage(this.Handle, Messages.WM_KEYDOWN, (int)key, 0);
}

private void SendKeyUp(Keys key)
{
    user32.SendMessage(this.Handle, Messages.WM_KEYUP, (int)key, 0);
}

This also ends up being converted to a \par

Is there a way to post a messaged directly to the msftedit control to insert a control character?

I am totally stumped, any ideas guys? Thanks for your help!

like image 447
Steve Sheldon Avatar asked Jan 06 '11 14:01

Steve Sheldon


People also ask

How do you insert a new line in RTF?

Re: INSERT NEW LINE IN RTF Solution that worked for me: I used ods escapechar='^'; I added ^n where I want my line break.

How do I make text bold in RTF?

In order to make the text bold you just need to surround the text with \b and use the Rtf member.

What is RichTextBox in C#?

In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc.

How do I write RTF code?

An RTF command consists of a backslash, then some characters a-z, and then an optional positive or negative integer argument. The command is then terminated either by a space, return, or linefeed (all of which are ignored), or by some character (like brace or backslash, etc.)


1 Answers

Adding a Unicode "Line Separator" (U+2028) does work as far as my testing showed:

private void Form_Load(object sender, EventArgs e)
{
    richText.AppendText("Hello, World!\u2028");
    richText.AppendText("Hello, World!\u2028");
    string rtf = richText.Rtf;
    richText.AppendText(rtf);
}

When I run the program, I get:

Hello, World!
Hello, World!
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
{\colortbl ;\red255\green255\blue255;}
\viewkind4\uc1\pard\cf1\f0\fs17 Hello, World!\line Hello, World!\line\par
}

It did add \line instead of \par.

like image 169
Peter Remmers Avatar answered Sep 16 '22 14:09

Peter Remmers