Search code examples
c#richtextboxwindows-forms-designer

RichTextBox bold text


I am filling a richtextbox with information gathered from Textboxes. I need that the input from the user gets Bold in the Richtextbox. I have tried different answers in SO, but or either I am doing something wrong or I couldn't find the right answer to this.

I've tried RTF and 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.";

Solution

  • Here is how you can Bold a portion of the sentence (a few words):

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

    Or if you want to Bold all the text:

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