Control.Font 屬性

定義

取得或設定控制項顯示之文字字型。Gets or sets the font of the text displayed by the control.

public:
 virtual property System::Drawing::Font ^ Font { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
public virtual System.Drawing.Font Font { get; set; }
member this.Font : System.Drawing.Font with get, set
Public Overridable Property Font As Font

屬性值

要套用至控制項顯示之文字的 FontThe Font to apply to the text displayed by the control. 預設值為 DefaultFont 屬性的值。The default is the value of the DefaultFont property.

範例

下列程式碼範例會向使用者顯示 FontDialog,並變更 DateTimePicker 控制項的 FontThe following code example displays a FontDialog to the user and changes the Font of a DateTimePicker control. 這個範例需要您具有 ButtonForm 和其上的 DateTimePickerThis example requires that you have a Form with Button and a DateTimePicker on it.

private:
   void myButton_Click( Object^ sender, EventArgs^ e )
   {
      FontDialog^ myFontDialog = gcnew FontDialog;
      if ( myFontDialog->ShowDialog() == ::DialogResult::OK )
      {
         // Set the control's font.
         myDateTimePicker->Font = myFontDialog->Font;
      }
   }
private void myButton_Click(object sender, EventArgs e)
{
   FontDialog myFontDialog = new FontDialog();
   if(myFontDialog.ShowDialog() == DialogResult.OK)
   {
      // Set the control's font.
      myDateTimePicker.Font = myFontDialog.Font;
   }
}
Private Sub myButton_Click(sender As Object, e As EventArgs)
   Dim myFontDialog As FontDialog
   myFontDialog = New FontDialog()
   
   If myFontDialog.ShowDialog() = DialogResult.OK Then
      ' Set the control's font.
      myDateTimePicker.Font = myFontDialog.Font
   End If
End Sub

備註

Font 屬性是環境屬性。The Font property is an ambient property. 環境屬性是控制項屬性,如果未設定,則會從父控制項抓取。An ambient property is a control property that, if not set, is retrieved from the parent control. 例如,Button 預設會有相同的 BackColor 做為其父系 FormFor example, a Button will have the same BackColor as its parent Form by default. 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或 Control 類別的總覽。For more information about ambient properties, see the AmbientProperties class or the Control class overview.

因為 Font 是不可變的(也就是說,您無法調整其任何屬性),所以您只能將 Font 屬性指派給新的 FontBecause the Font is immutable (meaning that you cannot adjust any of its properties), you can only assign the Font property a new Font. 不過,您可以用現有字型作為新字型的基礎。However, you can base the new font on the existing font.

[Visual Basic, C#][Visual Basic, C#]

以下是如何調整現有字型以使其變成粗體的範例:The following is an example of how to adjust the existing font to make it bold:

myControl.Font = new Font(myControl.Font,   
    myControl.Font.Style | FontStyle.Bold);  
MyControl.Font = New Font(MyControl.Font, _   
    MyControl.Font.Style Or FontStyle.Bold)  

給繼承者的注意事項

覆寫衍生類別中的 Font 屬性時,請使用基類的 Font 屬性來擴充基底的執行。When overriding the Font property in a derived class, use the base class's Font property to extend the base implementation. 否則,您必須提供所有的實作為。Otherwise, you must provide all the implementation. 您不需要覆寫 Font 屬性的 getset 存取子;您可以視需要覆寫其中一個。You are not required to override both the get and set accessors of the Font property; you can override only one if needed.

適用於

另請參閱