RichTextBox.SelectionFont 属性

定义

获取或设置当前选定文本或插入点的字体。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; }
member this.SelectionFont : System.Drawing.Font with get, set
Public Property SelectionFont As Font

属性值

表示应用到当前选定文本或在插入点后输入的文本的字体的 FontA Font that represents the font to apply to the current text selection or to text entered after the insertion point.

属性

示例

下面的代码示例将 RichTextBox 控件中的插入点后输入的文本选择或文本更改为当前字体加粗样式设置。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. 此示例要求代码包含在 Form 的方法中。This example requires that the code is contained within a method in a Form. 该示例还要求将名为 richTextBox1RichTextBox添加到 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 == true )
      {
         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 == true)
      {
         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

注解

如果当前选定文本包含多个指定的字体,则此属性 nullIf 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. 可以使用此属性更改 RichTextBox 中的文本的字体样式。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.

若要更改控件中文本的颜色,请使用 SelectionColor 属性。To change the color of the text in the control, use the SelectionColor property.

适用于

另请参阅