RichTextBox粗体字

时间:2019-05-13 05:20:11

标签: c# richtextbox windows-forms-designer

我正在用richtextbox填充从文本框收集的信息。我需要用户输入在Richtextbox中变为粗体。我在SO中尝试了不同的答案,但是或者我做错了什么,或者找不到正确的答案。

我尝试过RTF和附加文本:

rtbGeneratedText.Text += @"In answer to your request regarding " + 
    rtbSpecialRequest.Text +
    " for your booking " +
    bookingNumberTxt.Text +
    " the hotel has informed us that unfortunately it is not possible." + "\r\n" + 
    "Please let us know if this negative answer will affect your reservation in any way.";

1 个答案:

答案 0 :(得分:0)

以下是您加粗几个字的方法:

string target = "the hotel has informed us that unfortunately it is not possible";
RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(target);
RichTextBox1.SelectionLength = target.Length;
RichTextBox1.SelectionFont = new Font(RichTextBox1.Font, FontStyle.Bold);

或者如果您想粗体所有文本,

RichTextBox1.SelectionStart = 0;
RichTextBox1.SelectionLength = RichTextBox1.Length;
RichTextBox1.SelectionFont = new Font(RichTextBox1.Font, FontStyle.Bold);