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?
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.
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.
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.
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#.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With