MikeJ
06-04-2003, 09:12 PM
Hey all you VB programmers out there! This has been a common topic recently, so I decided to write a nice little tutorial on the Common Dialog control.
The first step in the common dialog control is to include it in your project. To do this, go to Project > Components > Microsoft Common Dialog Control 6.0 (SP 3).
After this, you will see a little box in your control toolbox. Click this and drag it onto your form. The default name is CommonDialog1.
The common dialog, contrary to some people’s beliefs, doesn’t actually do anything itself. It just is a easy to use pointer for other objects. The following dialogs can be show: ShowOpen, ShowSave, ShowColor, ShowFont, ShowPrinter, and ShowHelp. This tutorial will cover ShowOpen, ShowSave, ShowColor, and ShowFont, as they are probably the most used functions of the common dialog control.
There is a property called CancelError. If you set it to true, it will cause an error to occur whenever you press the cancel button. To correct this problem, use this code from alp0001:
On Error Goto cancel_error
'common dialog code here
Exit Sub
cancel_error:
If Err.Number <> cdlCancel Then
MsgBox Err.Description
End If
ShowOpen
The ShowOpen dialog, as with the ShowSave dialog, is the list of files and folders on your computer. If you have a Microsoft Office product, you are probably familiar with this dialog.
To open a text file with the Open dialog, you would need the following code: (This method uses the Microsoft Rich Text Box control.)
With CommonDialog1 'use this because we will have a lot of code with the box
.DialogTitle = "Open" 'this sets the caption for the title bar on the dialog
.InitDir = "C:\" 'this sets the initial director for the dialog box
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
'the filter property is what specifies which file types to display. The
'(*.txt) is not necessary.
'as you can see, the file name comes first then the *.type comes
'next after the |. This is important
.ShowOpen 'this event actually displays the open dialog box
End With 'Close the with statement
RTB1.LoadFile CommonDialog1.FileName
ShowSave
The ShowSave dialog, as well as the ShowOpen, lists the files and folders of your computer. You can make new folders with this dialog. You are probably familiar with this dialog if you have a Microsoft Office product.
To save a text file with the Save dialog, you need to use the following code: (This example also uses the Microsoft Rich Text Box Control)
With CommonDialog1 'open a with statement as there is a lot of code
.DialogTitle = "Save" 'sets the dialog title
.InitDir = "C:\" 'sets the initial directory
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 'sets the file
'types
.ShowSave 'show the dialog
End With
RTB1.SaveFile CommonDialog1.FileName 'save to the specified file
ShowColor
The Color dialog box allows a user to select a color from the specified color palette, or they can even make their own custom color.
To use the Color Dialog box to change the BackColor of the form, use this code: (You will need a Command Button named Command1)
Private Sub Command1_Click()
With CommonDialog1 'open a with block
.DialogTitle = "Color" 'set the title
.ShowColor 'show the dialog
End With
Form1.BackColor = CommonDialog1.Color 'set the backcolor to the
'color selected
End Sub
ShowFont
The Font Dialog box is the one with the most properties, but it is the most useful if you are making a text editor. (Or another app where the user needs to set the font.)
“The Font dialog box allows the user to select a font by specifying a font, size, color, and style.” – from MSDN.
The following code will change the font properties in a rich text box, which we will call RTB1.
On Error Resume Next 'allow for errors
With CommonDialog1 'open the with dialog
.DialogTitle = "Font" 'set the dialog title
.Flags = cdlCFBoth Or cdlCFEffects 'set the flags so you can
'access the fonts* and the strikethru, underline, color
.ShowFont 'show the dialog
End With
RTB1.SelFontName = CommonDialog1.FontName 'set the selected font’s name
RTB1.SelFontSize = CommonDialog1.FontSize 'set the selected font’s size
RTB1.SelBold = CommonDialog1.FontBold 'set the selected font’s bold property
RTB1.SelItalic = CommonDialog1.FontItalic 'set the selected font’s italic property
RTB1.SelStrikeThru = CommonDialog1.FontStrikethru 'set the selected font’s strikethrough property
RTB1.SelUnderline = CommonDialog1.FontUnderline 'set the selected font’s underline property
*The flags property for the Font Dialog has many options:
cdlCFBoth displays both the Screen's fonts and the Printer's fonts.
cdlCFPrinterFonts displays the fonts that the printer uses.
cdlCFScreenFonts displays the fonts that the screen has.
cdlCFEffects allows you to select underline, strikethrough, and color.
cdlCFTTOnly allows you to only display the True Type fonts.
As for a last thought, here is a link to EliteVB's tutorial on subclassing the common dialog box. (http://www.xtremevbtalk.com/showpost.php?p=1011717&postcount=15)
Also, I attached the source code for this tutorial.
Enjoy!
Hope this helps and that I didn’t leave anything out,
~MikeJ
The first step in the common dialog control is to include it in your project. To do this, go to Project > Components > Microsoft Common Dialog Control 6.0 (SP 3).
After this, you will see a little box in your control toolbox. Click this and drag it onto your form. The default name is CommonDialog1.
The common dialog, contrary to some people’s beliefs, doesn’t actually do anything itself. It just is a easy to use pointer for other objects. The following dialogs can be show: ShowOpen, ShowSave, ShowColor, ShowFont, ShowPrinter, and ShowHelp. This tutorial will cover ShowOpen, ShowSave, ShowColor, and ShowFont, as they are probably the most used functions of the common dialog control.
There is a property called CancelError. If you set it to true, it will cause an error to occur whenever you press the cancel button. To correct this problem, use this code from alp0001:
On Error Goto cancel_error
'common dialog code here
Exit Sub
cancel_error:
If Err.Number <> cdlCancel Then
MsgBox Err.Description
End If
ShowOpen
The ShowOpen dialog, as with the ShowSave dialog, is the list of files and folders on your computer. If you have a Microsoft Office product, you are probably familiar with this dialog.
To open a text file with the Open dialog, you would need the following code: (This method uses the Microsoft Rich Text Box control.)
With CommonDialog1 'use this because we will have a lot of code with the box
.DialogTitle = "Open" 'this sets the caption for the title bar on the dialog
.InitDir = "C:\" 'this sets the initial director for the dialog box
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
'the filter property is what specifies which file types to display. The
'(*.txt) is not necessary.
'as you can see, the file name comes first then the *.type comes
'next after the |. This is important
.ShowOpen 'this event actually displays the open dialog box
End With 'Close the with statement
RTB1.LoadFile CommonDialog1.FileName
ShowSave
The ShowSave dialog, as well as the ShowOpen, lists the files and folders of your computer. You can make new folders with this dialog. You are probably familiar with this dialog if you have a Microsoft Office product.
To save a text file with the Save dialog, you need to use the following code: (This example also uses the Microsoft Rich Text Box Control)
With CommonDialog1 'open a with statement as there is a lot of code
.DialogTitle = "Save" 'sets the dialog title
.InitDir = "C:\" 'sets the initial directory
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 'sets the file
'types
.ShowSave 'show the dialog
End With
RTB1.SaveFile CommonDialog1.FileName 'save to the specified file
ShowColor
The Color dialog box allows a user to select a color from the specified color palette, or they can even make their own custom color.
To use the Color Dialog box to change the BackColor of the form, use this code: (You will need a Command Button named Command1)
Private Sub Command1_Click()
With CommonDialog1 'open a with block
.DialogTitle = "Color" 'set the title
.ShowColor 'show the dialog
End With
Form1.BackColor = CommonDialog1.Color 'set the backcolor to the
'color selected
End Sub
ShowFont
The Font Dialog box is the one with the most properties, but it is the most useful if you are making a text editor. (Or another app where the user needs to set the font.)
“The Font dialog box allows the user to select a font by specifying a font, size, color, and style.” – from MSDN.
The following code will change the font properties in a rich text box, which we will call RTB1.
On Error Resume Next 'allow for errors
With CommonDialog1 'open the with dialog
.DialogTitle = "Font" 'set the dialog title
.Flags = cdlCFBoth Or cdlCFEffects 'set the flags so you can
'access the fonts* and the strikethru, underline, color
.ShowFont 'show the dialog
End With
RTB1.SelFontName = CommonDialog1.FontName 'set the selected font’s name
RTB1.SelFontSize = CommonDialog1.FontSize 'set the selected font’s size
RTB1.SelBold = CommonDialog1.FontBold 'set the selected font’s bold property
RTB1.SelItalic = CommonDialog1.FontItalic 'set the selected font’s italic property
RTB1.SelStrikeThru = CommonDialog1.FontStrikethru 'set the selected font’s strikethrough property
RTB1.SelUnderline = CommonDialog1.FontUnderline 'set the selected font’s underline property
*The flags property for the Font Dialog has many options:
cdlCFBoth displays both the Screen's fonts and the Printer's fonts.
cdlCFPrinterFonts displays the fonts that the printer uses.
cdlCFScreenFonts displays the fonts that the screen has.
cdlCFEffects allows you to select underline, strikethrough, and color.
cdlCFTTOnly allows you to only display the True Type fonts.
As for a last thought, here is a link to EliteVB's tutorial on subclassing the common dialog box. (http://www.xtremevbtalk.com/showpost.php?p=1011717&postcount=15)
Also, I attached the source code for this tutorial.
Enjoy!
Hope this helps and that I didn’t leave anything out,
~MikeJ