首页 > 解决方案 > RichTextBox 粗体文本

问题描述

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

我试过 RTF 和 appendtext:

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.";

标签: c#richtextboxwindows-forms-designer

解决方案


以下是如何将句子的一部分(几个词)加粗:

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

推荐阅读