Udostępnij za pośrednictwem


Font Handling for Visual Basic 6.0 Users

This topic compares font-handling techniques in Visual Basic 6.0 with their equivalents in Visual Basic 2008.

Conceptual Differences

Fonts in Visual Basic 6.0 are handled in two different ways: as font properties of forms and controls, or as a stdFont object.

In Visual Basic 2008, there is a single Font object: System.Drawing.Font. The Font property of a form or control takes a Font object as an argument.

Setting Font Properties

In Visual Basic 6.0, font properties can be set at run time, either by assigning a stdFont object or by setting the properties directly on the control; the two methods can be interchanged.

In Visual Basic 2008, the Font property of a control is read-only at run time—you cannot set the properties directly. You must instantiate a new Font object each time you want to set a property.

Font Inheritance

In Visual Basic 6.0, font properties have to be set individually for each control or form; using a stdFont object simplifies the process but still requires code.

In Visual Basic 2008, font properties are automatically inherited from their parent unless they are explicitly set for the child object. For example, if you have two label controls on a form and change the font property of the form to Arial, the label control's font also changes to Arial. If you subsequently change 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 supports raster fonts for backward compatibility; Visual Basic 2008 supports only TrueType and OpenType fonts.

Enumerating Fonts

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

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

Note

Visual Basic 6.0 enumerates all types of fonts. Visual Basic 2008 supports only TrueType and OpenType fonts; other font types are not enumerated. In addition, Visual Basic 6.0 enumerates each character-set version within a font family (for instance, Arial, Arial Baltic, Arial Greek); Visual Basic 2008 enumerates only the font families.

Code Changes for Fonts

The following code examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Code Changes for Setting Font Properties

The following example demonstrates setting font properties at run time. In Visual Basic 6.0, you can set properties directly on a control; in Visual Basic 2008, you must create a new Font object and assign it to the control each time you need to set a property.

' Visual Basic 6.0 
' Set font properties directly on the control.
Label1.FontBold = True
' Create a stdFont object.
Dim f As New stdFont
' Set the stdFont object to the Arial font.
f.Name = "Arial"
' Assign the stdFont to the control's font property.
Set Label1.Font = f
' You can still change properties at run time.
Label1.FontBold = True
Label1.FontItalic = True
' Visual Basic' Create a new Font object � Name and Size are required.Dim f AsNew System.Drawing.Font("Arial", 10)
' Assign the font to the control
Label1.Font = f
' To set additional properties, you must create a new Font object.
Label1.Font = New System.Drawing.Font(Label1.Font, FontStyle.Bold Or FontStyle.Italic)

Code Changes for Enumerating Fonts

The following example demonstrates filling a ListBox control with a list of the fonts installed on a computer.

Note

Visual Basic 6.0 enumerates all types of fonts. Visual Basic 2008 supports only TrueType and OpenType fonts; other font types are not enumerated. In addition, Visual Basic 6.0 enumerates each character-set version within a font family (for instance, Arial, Arial Baltic, Arial Greek); Visual Basic 2008 enumerates only the font families.

' Visual Basic 6.0 
Dim i As Integer
For i = 0 To Screen.FontCount – 1
   List1.AddItem Screen.Fonts(i)
Next i
' Visual Basic Dim ff As FontFamily
ForEach ff In System.Drawing.FontFamily.Families
  listBox1.Items.Add(ff.Name)
Next

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, any font-handling code is modified to use the new Font object.

Font inheritance in Visual Basic 2008 can cause unintended changes in the appearance of your application. You should check your converted application for any code that explicitly sets a font at the form or container level and, if necessary, change the font for any child controls that should not inherit that font.

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.

If your application contains code that enumerates fonts, raster fonts will not be enumerated in the upgraded application, and font families are enumerated rather than individual character-set versions.

See Also

Reference

Font

FontFamily..::.Families

Other Resources

Windows Forms Controls for Visual Basic 6.0 Users