gpt4 book ai didi

c# - 如何在富文本框中添加粗体文本

转载 作者:太空狗 更新时间:2023-10-29 22:22:37 29 4
gpt4 key购买 nike

我正在使用 C# 使用 Visual Studio Express 2012。

我正在使用代码将文本添加到 RichTextBox。每次添加 2 行。第一行要加粗,第二行要正常。

这是我唯一能想到的尝试,尽管我确信它不会起作用:

this.notes_pnl.Font = new Font(this.notes_pnl.Font, FontStyle.Bold);
this.notes_pnl.Text += tn.date.ToString("MM/dd/yy H:mm:ss") + Environment.NewLine;
this.notes_pnl.Font = new Font(this.notes_pnl.Font, FontStyle.Regular);
this.notes_pnl.Text += tn.text + Environment.NewLine + Environment.NewLine;

如何将粗体行添加到富文本框?

感谢您到目前为止提交的答案。我想我需要澄清一点。我没有添加这 2 行 1 次。我将多次添加这些行。

最佳答案

为了使文本加粗,您只需要用 \b 包围文本并使用 Rtf 成员。

this.notes_pln.Rtf = @"{\rtf1\ansi this word is \b bold \b0 }";

OP 提到他们将随着时间的推移添加线路。如果是这种情况,那么可以将其抽象为一个类

class RtfBuilder { 
StringBuilder _builder = new StringBuilder();

public void AppendBold(string text) {
_builder.Append(@"\b ");
_builder.Append(text);
_builder.Append(@"\b0 ");
}

public void Append(string text) {
_builder.Append(text);
}

public void AppendLine(string text) {
_builder.Append(text);
_builder.Append(@"\line");
}

public string ToRtf() {
return @"{\rtf1\ansi " + _builder.ToString() + @" }";
}
}

关于c# - 如何在富文本框中添加粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21211909/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com