Visual Basic Concepts
Using the Font Dialog Box
The Font dialog box allows the user to select a font by its size, color, and style. Once the user makes selections in the Font dialog box, the following properties contain information about the user's selection.
Property | Determines |
Color | The selected color. To use this property, you must first set the Flags property to cdlCFEffects. |
FontBold | Whether bold was selected. |
FontItalic | Whether italic was selected. |
FontStrikethru | Whether strikethrough was selected. |
FontUnderline | Whether underline was selected. |
FontName | The selected font name. |
FontSize | The selected font size. |
Figure 7.14 The Font dialog box
To display the Font dialog box
- Set the Flags property to one of the following Visual Basic constant values:
cdlCFScreenFonts (screen fonts)
cdlCFPrinterFonts (printer fonts)
cdlCFBoth (for both screen and printer fonts)
Caution You must set the Flags property to one of these values before displaying the Font dialog box. Otherwise, the error
No fonts exist
occurs.
- Use the ShowFont method to display the dialog box.
The following code sets the font properties for a text box based on a user's selections in the Font dialog box:
Private Sub Command1_Click ()
' Set Cancel to True.
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' Set the Flags property.
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
' Display the Font dialog box.
CommonDialog1.ShowFont
' Set text properties according to user's
' selections.
Text1.Font.Name = CommonDialog1.FontName
Text1.Font.Size = CommonDialog1.FontSize
Text1.Font.Bold = CommonDialog1.FontBold
Text1.Font.Italic = CommonDialog1.FontItalic
Text1.Font.Underline = CommonDialog1.FontUnderline
Text1.FontStrikethru = CommonDialog1.FontStrikethru
Text1.ForeColor = CommonDialog1.Color
Exit Sub
ErrHandler:
' User pressed Cancel button.
Exit Sub
End Sub
For More Information See "CommonDialog Control Constants" for a complete list of Flag property constants.