Richtextbox draw an rtf line

I want to add a horizontal line to the RichTextBox as a delimiter of my text. I've found some examples of RTF code implementing a line and tried them in that way:

rtbResFile.Rtf = @"{\rtf1{\pard some text.\par}{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}{\pard \par}{\pard some other text.\par}}";

This way implements creating a blank paragraph with border, so that should looks like a line. However it doesn't show anything. Just a blank paragraph. Even if I try to implement it in the way include line object

{\pard some text.\par}
{\pard {\*\do\dobxcolumn\dobypara\dodhgt
{\pard some other text.\par}

It still shows nothing. Does RichTextBox supports this? Or any other ways include the horizontal line in the rtf string?


There are a few different ways to create a horizontal line in RTF. Depending on the control or program being used your mileage may vary. RTF implementations in controls and programs tend to simply ignore markup that they don't know how to deal with.

By drawing polygons:

\dobxcolumn \dobypara \dodhgt7200
\dpline \dpptx0 \dppty0 \dpptx7200
\dppty0 \dpx0 \dpy0 \dpxsize7200
\dpysize0 \dplinew15
\dplinecor0 \dplinecog0 \dplinecob0 }\par}

By inserting a blank paragraph with a border followed by another blank paragraph without a border:

{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}

You can change the size and apparent positionof the line by setting indents on the paragraph:

{\pard \li2268 \ri567
\brdrb \brdrs \brdrw10 \brsp20 \par}

I highly recommend O'Reilly's RTF Pocket Guide for working with this stuff, which is where this came from.

Some further experimentation produced the code below, which does work in WordPad and the RichTextBox control.


Basically, it involves inserting a 1x1 pixel image of a black dot and stretching it as needed by adjusting the height and width goals. The goal measurement is in twips. A twip is defined as being 1/1440 of an inch. It's a horrible hack, but it works.

This function creates a horizontal bar that's just a picture. To create this picture, I just copied a horizontal bar from Visio into an RTF textbox, and then viewed the underlying RTF. Thus, it's possible to insert any image in this manner.

The code below works by moving the cursor to the very end of the text, then setting the "selected" RTF to be the aforementioned bar image. The text is then unselected.

The code sets this bar to be centered, however by setting the centreText to an empty string (or just removing the code), left alignment will be maintained.

    /// <summary>
    /// Appends a horizontal bar at the end of the specified Rich Text Box
    /// </summary>
    /// <param name="rtb">Rich Text Box to which horizontal bar is to be added</param>
    private void AppendHorizontalBar(RichTextBox rtb)
        // Position cursor at end of text
        rtb.Select(txtActivityLog.TextLength, 0);
        int selStart = rtb.TextLength;
        int selEnd = rtb.TextLength;

        // Textbox may transform chars, so (end-start) != text.Length
        rtb.Select(selStart, selEnd - selStart);

        // This is the RTF section to add.
        string horizontalBarRtf = @"{\pict\wmetafile8\picw12777\pich117\picwgoal7245\pichgoal60 0100090000035b00000004000800000000000400000003010800050000000b0200000000050000000c022100280e030000001e0008000000fa0200000300000000008000040000002d01000007000000fc020100000000000000040000002d010100080000002503020011001100170e110008000000fa0200000000000000000000040000002d01020007000000fc020000ffffff000000040000002d01030004000000f0010000040000002701ffff030000000000}";
        string centreText = "\\pard\\qc"; // set this to empty string to keep existing text alignment

        // Wrap to-add RTF section in RTF tag
        rtb.SelectedRtf = String.Format("{{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 {0} {1} \\line}}", centreText, horizontalBarRtf);

        // Leave no text selected
        rtb.SelectionLength = 0;

Need Your Help