RichTextBox.SelectionFont Property

Definition

Gets or sets the font of the current text selection or insertion point.

C#
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font SelectionFont { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font? SelectionFont { get; set; }

Property Value

A Font that represents the font to apply to the current text selection or to text entered after the insertion point.

Attributes

Examples

The following code example changes the current font bold style setting for the text selection or text entered after the insertion point within the RichTextBox control. This example requires that the code is contained within a method in a Form. The example also requires that a RichTextBox, named richTextBox1, has been added to the Form.

C#
private void ToggleBold()
{
   if (richTextBox1.SelectionFont != null)
   {
      System.Drawing.Font currentFont = richTextBox1.SelectionFont;
      System.Drawing.FontStyle newFontStyle;

      if (richTextBox1.SelectionFont.Bold)
      {
         newFontStyle = FontStyle.Regular;
      }
      else
      {
         newFontStyle = FontStyle.Bold;
      }

      richTextBox1.SelectionFont = new Font(
         currentFont.FontFamily, 
         currentFont.Size, 
         newFontStyle
      );
   }
}

Remarks

If the current text selection has more than one font specified, this property is null. If no text is currently selected, the font specified in this property is applied to the current insertion point and to all text that is typed into the control after the insertion point. The font setting applies until the property is changed to a different font or until the insertion point is moved to a different section within the control.

If text is selected within the control, the selected text and any text entered after the text selection will have the value of this property applied to it. You can use this property to change the font style of text in the RichTextBox. You can make the text in the control bold, italic, and underlined. You can also change the size of the text and the font applied to the text.

To change the color of the text in the control, use the SelectionColor property.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also