How to Append RTF Text in RichTextBox, Win C#

久未见 提交于 2019-12-23 14:59:07

问题


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?


回答1:


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.




回答2:


Assuming insertion point is at the end (it is, by default), just do this:

richTextBox1.SelectedRtf = @"{\rtf1\ansi Adding some \b text\b0.}";




回答3:


I found a solution this one worked for me

var rtb = new RichTextBox()
  { Rtf= obj.TOCOMM };
rtb.SelectAll();
rtb.Copy();
RTXT_DoText.Paste();



回答4:


Although I have seen lots of examples using the clipboard, which is a fantastic way to insert images anywhere in a Rich Text control (just use the Rich Text control's Paste() method), the easiest solution is to simply place your target SelectionStart property to the its TextLength property, ensure that its SelectionLength property is zero, and then stuff the contents of the source's SelectedRtf property to your target now-empty SelectedRtf. Of course, the caveat is that you should not try to instead insert the entire contents of a RTF property to the target Rtf property. You just want the selection. Sometimes I get around this in cases where I must do this by creating a hidden Rich Text control, stuff its Rtf property with the full RTF text to insert, invoke its SelectAll method to select all the text I want to insert, and then plug that RTB's SelectedRtf property to the target SelectedRtf property.



来源:https://stackoverflow.com/questions/22321456/how-to-append-rtf-text-in-richtextbox-win-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!