# CodeGuru Technical FAQs > CodeGuru Individual Visual Basic FAQs >  [VB6] How Do I Work With The CommonDialog Control?

## HanneSThEGreaT

*Q:* What is the CommonDialog control exactly?

*A:* The CommonDialog control is used to add Windows' dialogs to your application.  Dialogs you can add to your application include the following :

OpenSavePrintColorFontsPrintHelp

*Q:* What is the benefit of using this CommonDialog control over creating your own?

*A:* The real benefit of using this component is that you get access to the built in dialogs provided by Windows, thus, creating consitency with all the other applications' look and feel

*Q:* How do I add this control to my form?

*A:* The easiest way is to right click on your tyoolbox, and to select *Components...*  This will produce a box where you can select it.  Scroll down until you find [b]Microsoft Common Dialog _(Service pack version)_ Add a checkmark next to it and select OK.  Just a note : Service pack will represent the current Visual Basic service pack you have instaled.  So, if you have Service Pack 6 installed it will show *6.0*.  You will see the control gets added to your Toolbox.  Double click it, and it is part of your Form.  The CommonDialog control is actually a windowless control; this means that although you may see it during Design time, you will not see it appear on the form - you have to manipulate it through code, in order to show whichever dialog you want.

*Q:* How do I show the Open Dialog box?

*A:* Simple.  Add the following code :



```
Private Sub Command1_Click()
CommonDialog1.ShowOpen
End Sub
```

This simply shows the Open dialog.  It is actually ( at this moment ) not very useful, as it doesn't allow us to obtain whichever file was selected.  The proper way to show the Open Dialog box is :



```
Private Sub Command1_Click()
CommonDialog1.ShowOpen

If CommonDialog1.FileName <> "" Then
    Label1.Caption = CommonDialog1.FileName
End If

End Sub
```

Here we just look at the FileName property of our COmmonDialog to establish a file selection.  In case you expected the actual file to open, I have bad news  :Smilie:   This control doesn't physiaclly open the file for you, you still have to add the proper logic in order to achieve that.  This just acts as a gateway to your files - that is all

*Q:* How do I show the Save Dialog box?

*A:* Simple.  Add the following code :



```
Private Sub Command2_Click()
CommonDialog1.ShowSave

If CommonDialog1.FileName <> "" Then
    'Add Your File Saving Logic Here.
End If

End Sub
```

*Q:* OK, simple enough.  Isn't there a way I could specify a Default Diectory to save to / to open from?

*A:* Yes, there is.  Simply make use of the *InitDir* property :



```
Private Sub Form_Load()
	CommonDialog1.InitDir = "C:\HanneSThEGreaT"

End Sub
```

*Q:* Is there a way to change the *Files Of Type* in the Save / Open dialog box; so that I can only, say for example, open or save text files?

*A:* Yes.  Just make use of the Filter property :



```
Private Sub Form_Load()
	CommonDialog1.Filter = "Text (*.txt)|*.txt|All Files (*.*)|*.*"

End Sub
```

This will allow you to open or save Text Files or any other file.

*Q:* How do I Select a colour with the CommonDialog control?

*A:* Add the following code :



```
Private Sub Command3_Click()
CommonDialog1.ShowColor
Form1.BackColor = CommonDialog1.Color
End Sub
```

If you perhaps didn't select a colour, it defaults to Black

*Q:* I have seen a different color dialog box by Windows, it includes a lot more options, is it possible to show this colour dialog instead?

*A:* Yes. You need to set the *cdlCCFullOpen* CommonDialog Flag before opening the dialog box.  It is easier than it sounds :



```
Private Sub Command3_Click()
CommonDialog1.Flags = cdlCCFullOpen
CommonDialog1.ShowColor
Form1.BackColor = CommonDialog1.Color
End Sub
```

I will explain all the varioous Flags for all the dialogboxes later.

*Q:* How do I display the Fonts dialogbox?

*A:* Add this code :



```
Private Sub Command4_Click()
CommonDialog1.ShowFont

Label1.Font = CommonDialog1.FontName
Label1.FontBold = CommonDialog1.FontBold
Label1.FontItalic = CommonDialog1.FontItalic
Label1.FontUnderline = CommonDialog1.FontUnderline
Label1.FontStrikethru = CommonDialog1.FontStrikethru

Label1.FontSize = CommonDialog1.FontSize
End Sub
```

Quite straightforward  :Smilie: 

*Q:* How do I show the Print dialogbox?

*A:* 

```
Private Sub Command5_Click()
CommonDialog1.Copies = 20
CommonDialog1.FromPage = 2
CommonDialog1.Orientation = cdlLandscape
CommonDialog1.ToPage = 10
CommonDialog1.ShowPrinter
End Sub
```

*Q:* How do I show the Help dialogbox?

*A:* Here is a small example :



```
Private Sub Command6_Click()
CommonDialog1.HelpFile = "ACMAIN80.HLP"
CommonDialog1.HelpCommand = cdlHelpContents
CommonDialog1.ShowHelp  ' Display Visual Basic Help contents topic.

End Sub
```

The whole problem with the Help dialog is that you *must* have a help file already.  In the above example I made use of Visual Basic's Help file.

*Q:* You mentioned something about the CommonDialog's Flags property earlier; can I have a list of all the various Flags we can set for the CommonDialog control?

*A:* Yes. Of course.  I am attaching a list with this post.

*Q:* Is this all there is to the CommonDialog control?

*A:* For this example yes.  But, there are many more setting you can set to further customise your commondialog control, and as you gain experience you will see them.  I just made this simple introduction so that you can find your way further.  :Smilie: 

I hope it was helpful.  :Smilie:

----------

