Developer

Changing VB6's default control font

VB6 uses the MS Sans Serif font in 8 point size for its controls that support a Font property. However, you can change the font after the control is created. Here are two techniques that simplify this process.

Visual Basic 6 uses the MS Sans Serif font in 8 point size for its controls that support a Font property. However, you can change the font after the control is created. I'll show you two techniques that simplify this process.

You can use the first method at design time. Instead of selecting each control individually and changing its font, you can select a group of controls and change the font for all of them in one step.

You select multiple controls by dragging a box around them with the arrow tool or by pressing [Shift] and clicking them. When you select multiple controls, the property window displays the properties that all the selected controls have in common, and changing a property (Font in this case) changes it for all the selected controls. If you select multiple controls and don't see the Font property listed, it probably means that you inadvertently selected a control that doesn't support this property, such as a scroll bar.

The second method works at run time. You can place code in a form's Load event procedure that loops through all the controls on the form and sets the Font property as desired.

Private Sub Form_Load()

Dim c As Control
On Error Resume Next

For Each c In Form1.Controls
    c.Font.Name = "Times"
    c.Font.Size = 12
Next

End Sub

Note the use of On Error Resume Next; this prevents an error from occurring if the form contains a control that doesn't have a Font property.

Remember that changing the font, particularly its size, can have some unintended consequences. For example, a caption that fit nicely on a Command Button may suddenly be too big.

Note: If you change the Font property of the form itself, then any controls subsequently placed on the form will inherit that font. However, controls that are already on the form will not be affected.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

Editor's Picks

Free Newsletters, In your Inbox