How to make some text bold in a rich text box in C#

You should set the font after the selection to the original font.

If you want you can save the SelectionStart and SelectionLength and call the Select method to select the text again.

// Remember selection
int selstart = control.SelectionStart;
int sellength = control.SelectionLength;

// Set font of selected text
// You can use FontStyle.Bold | FontStyle.Italic to apply more than one style
control.SelectionFont = new Font(control.Font, FontStyle.Bold);

// Set cursor after selected text
control.SelectionStart = control.SelectionStart + control.SelectionLength;
control.SelectionLength = 0;
// Set font immediately after selection
control.SelectionFont = control.Font;

// Reselect previous text
control.Select(selstart, sellength);

this way the text stays selected, and the font afterwards is still correct.


Comments

  1. Sincere

    • 2015/8/30

    SelectionLength; // Set font of selected text // You can use FontStyle.Bold | FontStyle.Italic to apply more than one style control.

  2. Ferrara

    • 2016/3/26

    // Remember selection int selstart = control.SelectionStart; int sellength = control.SelectionLength; // Set font of selected text // You can use FontStyle.Bold | FontStyle.Italic to apply more than one style control.SelectionFont = new Font(control.Font, FontStyle.Bold); // Set cursor after selected text control.SelectionStart = control.SelectionStart + control.SelectionLength; control.SelectionLength = 0; // Set font immediately after selection control.SelectionFont = control.Font

  3. Stone

    • 2019/3/11

    to declare bold within the string. And \line creates a new line. You can also do underline, etc.

  4. Riva

    • 2021/3/31

    How do I make text bold in RichTextBox C#? if you append @”\rtf1\ansi” you can use \b and \b0 to declare bold within the string. And \line creates a new line. You can also do underline, etc.

  5. Francisco

    • 2018/6/22

    . Then use the “Selection” properties (SelectionAlignment, SelectionBackColor, SelectionFont, etc.) to set the properties for the selected text.

  6. Alfonso

    • 2020/1/22

    this.richTextBox1.AppendText (. "\r \r *these figures are assuming a "+. "12% return on your investment minus 4% inflation"); } 2). Use RTF to format the content, some hint for you: · '\b' and '\b0', this pair stands for bolden. · '\i' and '\0' , this pair stands for italicization. · '\par' stands for newline.

  7. Begu

    • 2018/9/9

    Step 1: Create a new windows forms application. Form1.cs. using System;; using System 

  8. Kaysen

    • 2015/4/3

    int length = richTextBox1.Text.Length; richTextBox1.AppendText((((Convert.ToString((str + Convert.ToString(": "))) + str1) + "\r") + " ")); richTextBox1.Select(length, str.Length); richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold); } } } }

  9. Odin

    • 2018/10/1

    Is there any code or tags I can create within the string so that I can Bolden or Italicize certain words? I would normally assume no way until I 

  10. Dhruv

    • 2016/2/9

    1. You can do this with the help of FontStyle class.just add a button in your form and name it Bold and create a click event for that. You have to use Rich textbox for this, you cannot do this with textbox. This code will convert the selected text to bold.

  11. Jagger

    • 2018/1/16

    I want to bolden the name, I have tried many code examples, but this is the closest I've got to it working: int length = textBox.Text.Length; textBox.Text += 

  12. Castiel

    • 2020/9/20

    Here is an extension method that overloads the AppendText method with a color parameter:. public static class RichTextBoxExtensions { public static void AppendText(this RichTextBox box, string text, Color color) { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor; } }

  13. Arjun

    • 2019/1/28

    To do this, we place the C# RichTextBox Control in the left panel and C# RichTextBox allows formatting the portion of the text in bold, 

  14. Alvarez

    • 2017/8/23

    1. You could use Extension method to switch between Regular Style and Bold Style as below: static class Helper { public static void SwtichToBoldRegular (this TextBox c) { if (c.Font.Style!= FontStyle.Bold) c.Font = new Font (c.Font, FontStyle.Bold); else c.Font = new Font (c.Font, FontStyle.Regular); } }

  15. Cole

    • 2016/10/21

    The problem with your code is that if you have 2 of the same words in the richtextbox that you want bold or whatever, the code will always only 

  16. Jayson

    • 2018/8/25

    Some tags have a simple all-or-nothing effect on the text but others might allow for variations. For example, the color tag needs to know 

  17. Christian

    • 2017/9/19

    For more information, see Rich Text Format (RTF) Specification, version 1.6. or use some text box functions: ( read some about TextBox class)

Comments are closed.

Recent Posts