Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
Control Class
Control Properties
 Font Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Control.Font Property

Gets or sets the font of the text displayed by the control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Visual Basic (Declaration)
<LocalizableAttribute(True)> _
Public Overridable Property Font As Font
Visual Basic (Usage)
Dim instance As Control
Dim value As Font

value = instance.Font

instance.Font = value
C#
[LocalizableAttribute(true)] 
public virtual Font Font { get; set; }
C++
[LocalizableAttribute(true)] 
public:
virtual property Font^ Font {
    Font^ get ();
    void set (Font^ value);
}
J#
/** @property */
public Font get_Font ()

/** @property */
public void set_Font (Font value)
JScript
public function get Font () : Font

public function set Font (value : Font)

Property Value

The Font to apply to the text displayed by the control. The default is the value of the DefaultFont property.

The Font property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default. For more information about ambient properties, see the AmbientProperties class or the Control class overview.

Because 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.

The following is an example of how to adjust the existing font to make it bold:

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

Notes to Inheritors 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. You are not required to override both the get and set accessors of the Font property; you can override only one if needed.

The following code example displays a FontDialog to the user and changes the Font of a DateTimePicker control. This example requires that you have a Form with Button and a DateTimePicker on it.

Visual Basic
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
C#
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;
   }
}
C++
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;
      }
   }
J#
private void myButton_Click(Object sender, EventArgs e)
{
    FontDialog myFontDialog = new FontDialog();
    if (myFontDialog.ShowDialog().Equals(get_DialogResult().OK)) {
        // Set the control's font.
        myDateTimePicker.set_Font(myFontDialog.get_Font());
    }
} //myButton_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Avoid overridding this property      David M. Kean   |   Edit   |   Show History

It is tempting to override this property in a base form similar to the following:

[C#]
 
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Samples
{
public class FormBase : Form
{
public override Font Font
{
get { return SystemFonts.MessageBoxFont; }
}
}
}


Unfortunately, due to a known bug [1], text boxes and other system-drawn controls (as opposed to Windows Forms-drawn controls) placed on the form to not draw themselves using the Font. This is despite the controls sizing themselves based on the Font size.

To work around this, explictly set the font in the constructor of the form:

[C#]
 
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Samples
{
public class FormBase : Form
{
public FormBase()
{
this.Font = SystemFonts.MessageBoxFont;
}
}
}

[1] https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115480

Tags What's this?: Add a tag
Flag as ContentBug
Bug: Changing the font size on a ListView will not always cause the column header height to change      Andy Pennell   |   Edit   |   Show History

If your ListView is in Details mode and you change the font, the column header will not have its height adjusted when running on XP (but will on Vista). To fix this, don't call EnableVisualStyles.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker