How to Append RTF Text in RichTextBox, Win C#(如何在 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+= @"
tf1ansi Adding Some Text0.}";
//
现在追加
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);
除了问题中要求的参数之外,还有其他参数(例如颜色),但这些参数构成了可以使用 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.
这篇关于如何在 RichTextBox 中添加 RTF 文本,Win C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 RichTextBox 中添加 RTF 文本,Win C#
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01