Library code snippets

Common Dialog Example

This example shows you how to use the Common Dialog control to display the Open, Save, Select Colour and Print dialog boxes.

First, add a Common Dialog control to your form (you will need to add the component to your project first by going to Project | Components) . Next, add a text box, and name it txtData. You now need to add some buttons to display the different dialog boxes. Name them cmdOpen, cmdSave, cmdColour, and cmdPrint, and set their captions appropriately. Finally, add the code below.

Private Sub cmdColour_Click()
    On Error GoTo errhandler
    CommonDialog1.CancelError = True
       
    ' Display the Colour dialog box
    CommonDialog1.ShowColor
    ' Set the forms background colour to the one selected
    Me.BackColor = CommonDialog1.Color
    Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "you cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub
Private Sub cmdOpen_Click()
On Error GoTo errhandler
    CommonDialog1.CancelError = True
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNPathMustExist + cdlOFNFileMustExist
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|RTF (*.rtf)|*.rtf|Text Files (*.txt)|*.txt"
   
    ' Display the Save dialog box
    CommonDialog1.Filename = ""
    CommonDialog1.ShowOpen
    txtData.Text = "File Selected: " & CommonDialog1.Filename
    Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "you cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub

Private Sub cmdPrint_Click()
    StopPrinting = False
' Set CancelError is True
On Error GoTo errhandler
   
    CommonDialog1.PrinterDefault = True
    CommonDialog1.CancelError = True

    ' Set flags
    CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
    CommonDialog1.ShowPrinter

    Printer.Print txtData.Text
    Printer.EndDoc
    Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "you cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub

Private Sub cmdSave_Click()
    On Error GoTo errhandler
    CommonDialog1.CancelError = True
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt + cdlOFNPathMustExist
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|RTF (*.rtf)|*.rtf|Text Files (*.txt)|*.txt"
    ' Specify default filter
   
    ' Display the Save dialog box
    CommonDialog1.Filename = ""
    CommonDialog1.ShowSave
    ' Set the label values
    txtData.Text = "File Selected: " & CommonDialog1.Filename
    Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "You cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub

Comments

  1. 10 Mar 2004 at 00:21

    Can you also add justification code to printing module.
    It would be of gr8 help


    thankz

  2. 23 Dec 2003 at 09:06

    Thanks James..

  3. 16 Nov 2003 at 15:41

    Thanks for posting this thing in here James.


    You have no clue how many times I go in to this particular page because
    I always type something backwards or simply misplace the snippet.


    It must be 120 times now I type ".txt|.txt (Text Files)" instead of "Text Files (.txt)|.txt"
    I guess thats proof one shouldn't type when having 36 cups of coffee.



    Thanks

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Common Dialog Example.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback