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
Related articles
Related discussion
-
Problem with migration to C# (CoCreateInstanceEx)
by LRollison (1 replies)
-
VB6 Problem Creating Shortcuts
by rb1177 (0 replies)
-
how can i open a file
by kyawswarhtun (0 replies)
-
how to save any one form what i want?
by blackguy (5 replies)
-
Build an MP3 Player
by soybees (4 replies)
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...
Can you also add justification code to printing module.
It would be of gr8 help
thankz
Thanks James..
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
This thread is for discussions of Common Dialog Example.