|Title||Use standard dialogs in Visual Basic 6|
|Description||This example shows how to use standard dialogs in Visual Basic 6.|
|Keywords||dialog, standard dialog, FontDialog, OpenFileDialog, SaveFileDialog, ColorDialog, use standard dialogs, Visual Basic, Visual Basic 6, VB, VB 6|
|Categories||Software Engineering, Controls|
To use a standard dialog, first add the Microsoft Common Dialog Controls to the Toolbox (open the Project menu and select Components). Then place a dialog on a form and set any properties that you want to customize. For example, you may want to set a file dialog's Filter property. You may also want to set CancelError = True.
Using a dialog at run time is a four step process.
- Initialize the dialog. Set dialog properties to show the current settings. For example, set a FontDialog's Font property to the current font. (Unfortunately this doesn't seem to work for color dialogs.)
- Display the dialog.
- Check the return result. If you set the dialog's CancelError property to True, then you can check Err.Number to see if the user selected a value.
- If the user selected a value, use the dialog's values to do something. For example, make the form use the font selected in a font dialog.
The example program lets the user select a file for opening, a file for saving, foreground and background colors, and a font. The following code shows how the program lets you select a file for opening.
' Select a file for opening.
Private Sub cmdOpenFile_Click()
cdFile.FileName = txtOpenFile.Text
' Display the dialog.
On Error Resume Next
' Check the result.
If Err.Number = cdlCancel Then Exit Sub
If Err.Number <> 0 Then
MsgBox "Error " & Format$(Err.Number) & _
" selecting a file to open." & vbCrLf & _
' Take action.
txtOpenFile.Text = cdFile.FileName