RichTextBox 粗体文本

问题描述 投票:0回答:2

我正在用从文本框收集的信息填充

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.";
c# richtextbox windows-forms-designer
2个回答
1
投票

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

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

0
投票
private void toolBold_Click(object sender, EventArgs e)
{
    //Toggles the 'Bold' of the selected text:
    richTextBox1.SelectionFont = richTextBox1.SelectionFont.Bold ?
        new Font(richTextBox1.SelectionFont, FontStyle.Regular) :
        new Font(richTextBox1.SelectionFont, FontStyle.Bold);
}
© www.soinside.com 2019 - 2024. All rights reserved.