Redigera

Dela via


RichTextBox.SelectionFont Property

Definition

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

public:
 property System::Drawing::Font ^ SelectionFont { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font SelectionFont { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font? SelectionFont { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionFont : System.Drawing.Font with get, set
Public Property SelectionFont As Font

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.

void ToggleBold()
{
   if ( richTextBox1->SelectionFont != nullptr )
   {
      System::Drawing::Font^ currentFont = richTextBox1->SelectionFont;
      System::Drawing::FontStyle newFontStyle;
      if (richTextBox1->SelectionFont->Bold)
      {
         newFontStyle = FontStyle::Regular;
      }
      else
      {
         newFontStyle = FontStyle::Bold;
      }
      richTextBox1->SelectionFont = gcnew System::Drawing::Font( currentFont->FontFamily,currentFont->Size,newFontStyle );
   }
}
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
      );
   }
}
Private Sub ToggleBold()
   If richTextBox1.SelectionFont IsNot Nothing Then
      Dim currentFont As System.Drawing.Font = richTextBox1.SelectionFont
      Dim newFontStyle As System.Drawing.FontStyle

      If richTextBox1.SelectionFont.Bold = True Then
         newFontStyle = FontStyle.Regular
      Else
         newFontStyle = FontStyle.Bold
      End If

      richTextBox1.SelectionFont = New Font( _
         currentFont.FontFamily, _
         currentFont.Size, _
         newFontStyle _
      )
   End If
End sub

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

See also