This documentation is archived and is not being maintained.

Font Changes in Visual Basic .NET

Fonts in Visual Basic 6.0 were handled in two different ways: as font properties of forms and controls, or as a stdFont object. In Visual Basic .NET, there is a single Font object: System.Drawing.Font.

Setting Font Properties

In Visual Basic 6.0, font properties could be set at run time either by assigning a stdFont object or by setting the properties directly:

' Visual Basic 6.0 
' Uses the stdFont object.
Dim f As New stdFont
' Sets the stdFont object to the Arial font.
f.Name = "Arial"
' Assigns the stdFont to the font property.
Set Label1.Font = f
' Can still change properties at run time.
Label1.FontBold = True
Label1.Font.Italic = True

In Visual Basic .NET, fonts are read-only at run time — you cannot set the properties directly, but you can assign a new Font object:

' Visual Basic .NET 
' Fails – property is read-only.
label1.Font = Arial
' Assign a Font object — Name and Size are required.
label1.Font = New System.Drawing.Font("Arial", 10)
' Assign additional attributes.
label1.Font = New System.Drawing.Font(label1.Font, FontStyle.Bold __
    Or FontStyle.Italic)

Font Inheritance

In Visual Basic 6.0, font properties had to be set individually for each control or form. In Visual Basic .NET, font properties are automatically inherited from their parent unless they are explicitly set for the child object. For example, if you had two label controls on a form and changed the font properties of the form to Arial, the label control's fonts would also change to Arial. If you subsequently changed the font of one label to Times Roman, further changes to the form's font would not override the label's font.

Font Compatibility

Visual Basic 6.0 supported raster fonts for backward compatibility; Visual Basic .NET supports only TrueType and OpenType fonts. During upgrade, raster fonts are converted to the default OpenType font, Microsoft Sans Serif. Formatting such as Bold or Italic is not preserved. For more information, see Only OpenType and TrueType fonts are supported.

Enumerating Fonts

In Visual Basic 6.0, you could use the Screen.Fonts collection along with Screen.FontCount property to enumerate the available screen fonts.

In Visual Basic .NET, the Screen object no longer exists; in order to enumerate available fonts on the system you should use the System.Drawing.FontFamily namespace.

' Visual Basic 6.0 
Dim i As Integer
For i = 0 To Screen.FontCount – 1
   List1.AddItem Screen.Fonts(i)
Next i

' Visual Basic .NET 
Dim ff As FontFamily
For Each ff In System.Drawing.FontFamily.Families
   listBox1.Items.Add(ff.Name)
Next
Note   Visual Basic 6.0 enumerated all types of fonts. Visual Basic .NET only supports TrueType and OpenType fonts; other font types will not be enumerated. In addition, Visual Basic 6.0 enumerated each character set version within a font family (for instance, Arial, Arial Baltic, Arial Greek); Visual Basic .NET enumerates the font families.

See Also

Font Class | FontFamily.Families Property | Control Changes in Visual Basic .NET | UserControl Changes in Visual Basic .NET

Show: