简体   繁体   English

如何在RichTextBox中附加RTF文本,Win C#

[英]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. 我在Win C#中有一个RichTextBox,我想在RichTextBox中附加一些带有Bold效果的新文本。 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? 那我该怎么做呢?

The following function takes a reference to a RichTextBox, along with some formatting parameters. 以下函数引用了RichTextBox以及一些格式化参数。 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: ...然后在最后添加一个句子,以便Rich Text Box的内容符合要求:

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. 除了问题中要求的内容之外还有其他参数(例如颜色),但这些参数构成了所有格式化操作的基础,可以使用select-format-deselect方法进行格式化,而不是手动编辑RTF代码。

假设插入点位于末尾(默认情况下),只需执行以下操作:

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

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. 虽然我已经看到很多使用剪贴板的例子,这是在Rich Text控件中任意位置插入图像的绝佳方式(只需使用Rich Text控件的Paste()方法),最简单的解决方案是简单地将目标SelectionStart属性放入它的TextLength属性确保其SelectionLength属性为零,然后将源的SelectedRtf属性的内容填充到目标 - 现在为空的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. 当然,需要注意的是,您不应该尝试将RTF属性的全部内容插入到目标Rtf属性中。 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. 有时候,我必须通过创建隐藏的Rich Text控件来完成此操作,填充其Rtf属性并插入完整的RTF文本,调用其SelectAll方法以选择我要插入的所有文本,然后插入RTB的SelectedRtf属性为目标SelectedRtf属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM