Export (0) Print
Expand All

RichTextBox.SelectionFont Property

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public Font SelectionFont { get; set; }
/** @property */
public Font get_SelectionFont ()

/** @property */
public void set_SelectionFont (Font value)

public function get SelectionFont () : Font

public function set SelectionFont (value : Font)

Property Value

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

If the current text selection has more than one font specified, this property is a null reference (Nothing in Visual Basic). 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.

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.

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

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

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

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

        if (richTextBox1.get_SelectionFont().get_Bold() == true) {
            newFontStyle = FontStyle.Regular;
        }
        else {
            newFontStyle = FontStyle.Bold;
        }

        richTextBox1.set_SelectionFont(
            new Font(currentFont.get_FontFamily(), 
            currentFont.get_Size(), newFontStyle));
    }
} //ToggleBold

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft