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

屬性值

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 == 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

備註

如果目前的文字選取範圍指定了一個以上的字型,則此屬性為 null 。 如果目前未選取任何文字,則此屬性中指定的字型會套用至目前的插入點,以及插入點之後輸入控制項的所有文字。 字型設定會套用到屬性變更為不同的字型,或直到插入點移到控制項內的不同區段為止。

如果在控制項內選取文字,則選取的文字和在文字選取範圍之後輸入的任何文字都會套用此屬性的值。 您可以使用這個屬性來變更 中的 RichTextBox 文字字型樣式。 您可以將控制項中的文字設為粗體、斜體和底線。 您也可以變更文字的大小,以及套用至文字的字型。

若要變更 控制項中文字的色彩,請使用 SelectionColor 屬性。

適用於

另請參閱