Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Append RTF Text in RichTextBox, Win C#

I have a RichTextBox in Win C#, and I want to append some new text with Bold effect in RichTextBox. So how can i do this.

I tried

string str = richTextBox.Rtf;

//my logic
str+= @"\rtf1\ansi Adding Some \b Text\b0.}";
//

Now Appending

richTextbox.AppendText(str);

But its not showing the correct.

My Output before

This is First Word.

and i want output like

This is First Word. Adding Some Text.

So how can I do this?

like image 649
VarunJi Avatar asked Mar 11 '14 09:03

VarunJi


People also ask

How do I add text to RichTextBox?

Step 2: Drag the RichTextBox control from the ToolBox and drop it on the windows form. You are allowed to place a RichTextBox control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the RichTextBox control to add text in the RichTextBox control.

What is difference between RichTextBox and TextBox?

The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.

What is RTF extension?

Rich Text Format (RTF) is a file format that lets you exchange text files between different word processors in different operating systems (OSes). For example, you can create a file in Microsoft Word and then open it in another word processor, such as Apple Pages or Google Docs.

What is Rich Text Box?

The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.


1 Answers

The following function takes a reference to a RichTextBox, along with some formatting parameters. The function is documented:

/// <summary>
/// Append formatted text to a Rich Text Box control 
/// </summary>
/// <param name="rtb">Rich Text Box to which horizontal bar is to be added</param>
/// <param name="text">Text to be appended to Rich Text Box</param>
/// <param name="textColour">Colour of text to be appended</param>
/// <param name="isBold">Flag indicating whether appended text is bold</param>
/// <param name="alignment">Horizontal alignment of appended text</param>
private void AppendFormattedText(RichTextBox rtb, string text, Color textColour, Boolean isBold, HorizontalAlignment alignment)
{
    int start = rtb.TextLength;
    rtb.AppendText(text);
    int end = rtb.TextLength; // now longer by length of appended text

    // Select text that was appended
    rtb.Select(start, end - start);

    #region Apply Formatting
    rtb.SelectionColor = textColour;
    rtb.SelectionAlignment = alignment;
    rtb.SelectionFont = new Font(
         rtb.SelectionFont.FontFamily,
         rtb.SelectionFont.Size,
         (isBold ? FontStyle.Bold : FontStyle.Regular));
    #endregion

    // Unselect text
    rtb.SelectionLength = 0;
}

The following code adds the original text:

This is First Word.

// This creates the original text
AppendFormattedText(richTextBox, "This is ", Color.Black, false, HorizontalAlignment.Left);
AppendFormattedText(richTextBox, "First", Color.Black, true, HorizontalAlignment.Left);
AppendFormattedText(richTextBox, " Word.", Color.Black, false, HorizontalAlignment.Left);

... and then appends a sentence to the end, such that the content of the Rich Text Box is as desired:

This is First Word. Adding Some Text.

// This appends additional text
AppendFormattedText(richTextBox, " Adding Some ", Color.Black, false, HorizontalAlignment.Left);
AppendFormattedText(richTextBox, "Text", Color.Black, true, HorizontalAlignment.Left);
AppendFormattedText(richTextBox, ".", Color.Black, false, HorizontalAlignment.Left);

There are additional parameters (such as colour) that are in addition to what was asked for in the question, but these form the basis of all formatting operations that can be done with the select-format-deselect approach to formatting, rather than manually editing the RTF codes.

like image 110
CJBS Avatar answered Oct 09 '22 10:10

CJBS