What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse standard dialogs in Visual Basic 6
DescriptionThis example shows how to use standard dialogs in Visual Basic 6.
Keywordsdialog, standard dialog, FontDialog, OpenFileDialog, SaveFileDialog, ColorDialog, use standard dialogs, Visual Basic, Visual Basic 6, VB, VB 6
CategoriesSoftware 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.

  1. 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.)
  2. Display the dialog.
  3. 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.
  4. 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()
    ' Initialize.
    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 & _
        Exit Sub
    End If

    ' Take action.
    txtOpenFile.Text = cdFile.FileName
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.