我正在使用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;
Run Code Online (Sandbox Code Playgroud)
如何将粗体线添加到富文本框?
感谢到目前为止提交的答案.我想我需要澄清一点.我没有添加这两行1次.我将多次添加这些行.
为了使文本变粗,您只需要用文本包围\b并使用该Rtf成员.
this.notes_pln.Rtf = @"{\rtf1\ansi this word is \b bold \b0 }";
Run Code Online (Sandbox Code Playgroud)
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() + @" }";
}
}
Run Code Online (Sandbox Code Playgroud)