PDA

Click to See Complete Forum and Search --> : VB6 FAQ for Common Dialog Control


koolsid
Jun 17th, 2010, 03:43 AM
This tutorial is about the Common Dialog Control (CDC) which provides a standard interface for operations such as opening, saving, printing, selecting colours/fonts and for showing Help using the Microsoft Windows dynamic link library COMMDLG.DLL.

To use a Common Dialog Control, add "Microsoft Common Dialog Control" from the "Project", "Components" menu item. See picture below.

http://www.vbforums.com/attachment.php?attachmentid=78675&stc=1&d=1276763474

http://www.vbforums.com/attachment.php?attachmentid=78677&stc=1&d=1276763488

This will add the control the the Tool Box as shown below.

http://www.vbforums.com/attachment.php?attachmentid=78676&stc=1&d=1276763481

The best part is that CDC is visible on the form as a small icon at design-time but will be invisible at run-time.

Like I mentioned above, the CDC can be used for the following operations.

Open File using .ShowOpen
Save File using .ShowSave
Setting Color using .ShowColor
Setting Font using .ShowFont
Print using .ShowPrinter
Display Help using .ShowHelp


Lets cover each of them by creating a small Project.

Setting up the form

Create a form and add 6 buttons and set their captions as shown in the picture below

http://www.vbforums.com/attachment.php?attachmentid=78678&stc=1&d=1276763495

Place the CDC in the form as shown above
And lastly put a Rich Text Box. (To use the Rich Text Box Control, add "Microsoft Rich TextBox Control" from the "Project", "Components" menu item.)


Setting up the code

Paste the code below and then simply run the form. I have commented the code so you won’t have any difficulty understanding it :)

'~~> ShowOpen Example
Private Sub Command1_Click()
On Error GoTo cancel_error

With CommonDialog1
'~~> Set the Title of the Dialog Box
.DialogTitle = "Open"
'~~> Set the Initial directory
.InitDir = "C:\"
'~~> Set the Filter for the files that you want to open
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
'~~> Displays the Open Dialog Box
.ShowOpen
End With

'~~> Load the Rich Text Box with the text file
RichTextBox1.LoadFile CommonDialog1.FileName
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub

'~~> ShowSave Example
Private Sub Command2_Click()
On Error GoTo cancel_error

With CommonDialog1
'~~> Set the Title of the Dialog Box
.DialogTitle = "Save"
'~~> Set the Initial directory
.InitDir = "C:\"
'~~> Set the Filter for the files that you want to save as
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
'~~> Displays the Save Dialog Box
.ShowSave
End With

'~~> Save the Rich Text Box contents as a text file
RichTextBox1.SaveFile CommonDialog1.FileName
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub

'~~> ShowColor Example
Private Sub Command3_Click()
On Error GoTo cancel_error

With CommonDialog1
.DialogTitle = "Color"
'~~> Show the Color Dialog box
.ShowColor
End With

'~~> Set the back color of the Dialogbox
RichTextBox1.BackColor = CommonDialog1.Color
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub

'~~> ShowFont Example
Private Sub Command4_Click()
On Error GoTo cancel_error

With CommonDialog1
.DialogTitle = "Font"

'~~> Various flags that you can use
' cdlCFBoth ~~> Display Screen's and Printer's fonts
' cdlCFEffects ~~> For Underline, FontStrikethru, Color
' cdlCFTTOnly ~~> Display True Type fonts
' cdlCFScreenFonts ~~> Displays Screen fonts
' cdlCFPrinterFonts ~~> Displays Printer fonts

.Flags = cdlCFBoth Or cdlCFEffects

'~~> Show the Font Dialog box
.ShowFont
End With

'~~> Select the entire text in the Rich Text Box
'~~> Comment the below two lines if you want to
'~~> Manually select words in the Rich Text Box
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = Len(RichTextBox1)

'~~> Change Font
RichTextBox1.SelFontName = CommonDialog1.FontName
'~~> Change Font Size
RichTextBox1.SelFontSize = CommonDialog1.FontSize
'~~> Bold Font
RichTextBox1.SelBold = CommonDialog1.FontBold
'~~> Italicize Font if ticked
RichTextBox1.SelItalic = CommonDialog1.FontItalic
'~~> Underline if ticked
RichTextBox1.SelUnderline = CommonDialog1.FontUnderline

'~~> Strike through Font if ticked
RichTextBox1.SelStrikeThru = CommonDialog1.FontStrikethru
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub

'~~> ShowPrinter Example
Private Sub Command5_Click()
On Error GoTo cancel_error

'~~> Show the printer dialog box
CommonDialog1.ShowPrinter

'~~> Print the text in the Rich Text Box
Printer.Print RichTextBox1.Text
Printer.EndDoc
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub

'~~> ShowHelp Example
Private Sub Command6_Click()
On Error GoTo cancel_error

'~~> Set the help file
CommonDialog1.HelpFile = "MyHelp.hlp"
CommonDialog1.HelpCommand = cdlHelpContents
'~~> Show the Help File
CommonDialog1.ShowHelp
Exit Sub

cancel_error:
If Err.Number <> vbCancel Then
MsgBox Err.Description
End If
End Sub


I am also attaching the Zipped Project…

Hope this helps… :wave:

fafalone
Jul 17th, 2010, 08:42 PM
Since I just dealt with this issue myself, I thought I'd add a few things.

If you want the Open/Save dialogs to look like they normally do in Windows programs and not like a Win 3.1 nightmare, you'll need to do the following:

Const cdlOFNExplorer = &H80000

then when setting up the dialog,

.flags = cdlOFNExplorer


Also, to open multiple files:
Const cdlOFNAllowMultiselect = &H200

.flags = cdlOFNExplorer Or cdlOFNAllowMultiselect

(assuming you want the modern dialog too, if not, just the multiselect)

The documentation states that multiple names in .Filename are space delimited; however this is not true. They are null-character delimited.
So, to get at all the names, you'll need

Split(.Filename, vbNullChar)

The resulting array's first value (index 0) will be the path, without the filename, then 1 through ubound will be the names of files selected in that folder. Maybe it's just my system, but after opening multiple files and invoking the dialog again, the default name is set to a part of one of the previous file names, so if that happens just clear it by setting .Filename = "" before .Show... .

For all the flags, see: http://msdn.microsoft.com/en-us/library/aa259317%28v=VS.60%29.aspx (http://msdn.microsoft.com/en-us/library/aa259317%28v=VS.60%29.aspx)