共用方式為


RichTextBox.SelectionFont 屬性

定義

取得或設定當前文字選取或插入點的字型。

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

屬性值

A Font 代表應套用於當前文字選擇或插入點後輸入的字型。

屬性

範例

以下程式碼範例改變了目前字型粗體樣式設定,適用於控制項中 RichTextBox 插入點後的文字選擇或文字。 此範例要求程式碼包含在 Form。 範例還要求一個 RichTextBox名為 richTextBox1、 的 被加入 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

備註

如果目前的文字選擇指定了多個字型,這個屬性就是 null。 若目前未選取任何文字,則此屬性中指定的字型會套用到目前插入點及插入點之後輸入控制項的所有文字。 字型設定會持續使用,直到屬性被更改為其他字型,或插入點被移至控制項內的其他區塊為止。

若在控制項內選取文字,所選文字及後輸入的任何文字都會套用此屬性的值。 你可以利用這個屬性來更改 RichTextBox. 你可以把控制項裡的文字調成粗體、斜體或底線。 你也可以調整文字大小和套用的字體。

要更改控制項中文字的顏色,請使用屬性。SelectionColor

適用於

另請參閱