Using the CommonDialog control
The CommonDialog control provides a set of standard dialog boxes for operations such as opening and saving files, setting printing options, and selecting colors and fonts. The control can also display help when the Windows Help engine is running.
The CommonDialog control provides an interface between Visual Basic and the Microsoft Windows dynamic link library Commdlg.dll routine. In order to create a dialog box with this control, Commdlg.dll must be in the MicrosoftWindows/System directory.
In order to use the CommonDialog control in your application, you should add it to the form and set its properties. The dialog displayed by the control is determined by the control's methods. During runtime, the dialog box or help engine will be displayed after calling the corresponding method; during design, the CommonDialog control will be displayed as an icon on the form. The size of this icon cannot be changed.
The CommonDialog control can display the following common dialog boxes:
"Open"
"Save as"
"color"
"Font"
"Print"
To use the CommonDialog control
1. If the CommonDialog control is not added, you should select "Components" from the "Project" menu and add the control to the toolbox. Find and select the control in the "Controls" of the marking dialog, and then click the "OK" button.
2. Click the "CommonDialog" control in the toolbox and draw the control on the form. When you draw a CommonDialog control on a form, the control will automatically resize. Like the Timer control, the CommonDialog control is not visible at run time.
3. When running, please use the methods listed in the following table appropriately to display the required dialogue.
Display the Open and Save As dialog boxes
The Open dialog box lets you specify the drive, directory, file extension, and file name. The Save As dialog box is identical in appearance to the Open dialog box, except that the dialog box title and file name are dimmed. After selecting a file and closing the dialog box at runtime, you can use the FileName property to obtain the selected file name.
To display the Open dialog box
1. Specify the list of file filters displayed in the File Types list box.
The Filter property can be set in the following format:
description1|filter1|description2|filter2...
Description is the string displayed in the list box - for example, TextFiles(*.txt). Filter is an actual file filter - for example, *.txt. Each description|filter setting must be separated by a pipe symbol (|).
2. Use the ShowOpen method to display the dialog box.
After selecting a file, you can use the FileName property to get the name of the selected file.
For all public dialog boxes, an error is generated when the CancelError property is True and the user clicks the dialog box's Cancel button. Catch an error when displaying the dialog box to detect whether the Cancel button was pressed.
The following code displays the Open dialog box and opens the parameters of the file procedure with the selected file name:
PRivateSubmnuFileOpen_Click()
'CancelError is True.
OnErrorGoToErrHandler
'Set filter.
CommonDialog1.Filter=AllFiles(*.*)|*.*|Text_
Files(*.txt)|*.txt|BatchFiles(*.bat)|*.bat
'Specify the default filter.
CommonDialog1.FilterIndex=2
'Display the Open dialog box.
CommonDialog1.ShowOpen
'Call the procedure to open the file.
OpenFile(CommonDialog1.FileName)
ExitSub
ErrHandler:
'The user presses the "Cancel" button.
ExitSub
EndSub
Using the Color dialog box
Use the Color dialog box to select a color from a color palette or to create and select a custom color. At runtime, the Color property can be used to obtain the selected color after selecting the color and closing the dialog box.
To display the Color dialog box
1. Set the Flags property of the CommonDialog control to the VisualBasic constant cdlCCRGBInit.
2. Use the ShowColor method to display the dialog box.
You can use the Color property to get the RGB value of the selected color. The following code displays the Color dialog box when you click the Command1 command button:
PrivateSubCommand1_Click()
'Set Cancel to True.
CommonDialog1.CancelError=True
OnErrorGoToErrHandler
'Set the Flags property.
CommonDialog1.Flags=cdlCCRGBInit
'Display the Color dialog box.
CommonDialog1.ShowColor
'Sets the form's background color to the selected' color.
Form1.BackColor=CommonDialog1.Color
ExitSub
ErrHandler:
'The user pressed the "Cancel" button.
ExitSub
EndSub
Using the Font dialog box
The Font dialog box selects fonts based on size, color, and style. Once the user selects a font in the Font dialog box, the following properties contain information about the user's options.
To display the Font dialog box
1. Set the Flags property to one of the following Visual Basic constants:
cdlCFScreenFonts (screen fonts)
cdlCFPrinterFonts (printer fonts)
cdlCFBoth (can be either a screen font or a printer font)
Warning You must set the Flags property to one of these values before displaying the Font dialog box, otherwise a font not present error will occur.
2. Use the ShowFont method to display the dialog box.
The following code sets the font properties of a text box based on the user's selections in the Font dialog box:
PrivateSubCommand1_Click()
'Set Cancel to True.
CommonDialog1.CancelError=True
OnErrorGoToErrHandler
'Set the Flags property.
CommonDialog1.Flags=cdlCFBothOrcdlCFEffects
'Display the "Font" dialog box.
CommonDialog1.ShowFont
'Set text properties based on user 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
ExitSub
ErrHandler:
'The user pressed the "Cancel" button.
ExitSub
EndSub
->