如何在WinC#中在RichTextBox中附加RTF文本 [英] How to Append RTF Text in RichTextBox, Win C#

查看:436
本文介绍了如何在WinC#中在RichTextBox中附加RTF文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Win C#中有一个RichTextBox,我想在RichTextBox中添加一些带有粗体效果的新文本。那么我该怎么做。

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.

我尝试过

string str = richTextBox.Rtf;

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

现在追加中

richTextbox.AppendText(str);

但显示不正确。

我之前的输出


这是第一单词。

,而我想要输出


这是 First 字。添加一些文本

那么我该怎么做?

推荐答案

以下函数将参考RichTextBox以及一些格式设置参数。该函数记录如下:

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:


这是 First 字。



// 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);

...,然后将句子添加到末尾,以使Rich Text Box的内容根据需要:

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


这是第一字。添加一些文本



// 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);

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

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.

这篇关于如何在WinC#中在RichTextBox中附加RTF文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆