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

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

问题描述

我在 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);

但它没有显示正确.

我之前的输出

这是第一个词.

我想要像

这是第一个词.添加一些文本.

那我该怎么做呢?

推荐答案

以下函数接受对 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;
}

以下代码添加原文:

这是第一个词.

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

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

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

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