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. 此示例要求具有 Form,其中包含 ButtonDateTimePickerThis 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 将具有与其父 Form 相同的 BackColorFor 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.

适用于

另请参阅