The Common Dialog control is used to display dialog boxes commonly found in windows applications (hence the name!) Some common dialogs include: Open File, Save File, Print File, and Color.

Installing the Common Dialog Box

The Common Dialog box may NOT be your standard VB toolbox. It looks like this:

If it is NOT installed, follow these steps.

bullet Select Components from the Project Menu (or press Control-T), You will see the Components Dialog box as shown below. Make sure the Controls tab is selected.

bullet Scroll Down and check Microsoft Common Dialog Control 6.0

bullet

Click OK

You should now see the control in your toolbox.

The Many Methods of the Dialog Box

The common dialog box is one of the most complex controls in Visual Basic. The various behaviors of the box is controlled by the following methods:

Open File .ShowOpen
Save File .ShowSave
Color .ShowColor
Print .ShowPrinter
Help .ShowHelp

To experiment with some of these methods.

Step 1: Create the following interface.

Step 2: Set the Properties

There is nothing special about the text box and six buttons other than the fact that a vertical scroll bar was added to the text box. Name the controls: 

bullet txtDisplay
bullet cmdOpenFile
bullet cmdSaveFile
bullet cmdFont
bullet cmdPrint
bullet cmdTextColor
bullet cmdBackColor

Step 3: Write the code

The Font Dialog

Error trapping

Visual Basic allows you to write code that handles errors. The method is based on the traditional programming idea of a "flag." The computer raises an error flag when an error occurs. Your VB code can then intercept the error before the program crashes. To use error trapping, follow the following steps.

Tell the computer where the error code is On Error GoTo label name
Tell the computer what to do if there is an error label name:

 As it turns out, Visual Basic considers selecting Cancel from a dialog box

Example:

Sub cmdFont_Click( )

CommonDialog1.CancelError = False 'set the error flag
On Error GoTo FontError ‘where to go

…The code goes here

… and here

FontError: ‘this is a label

Exit Sub

FontError:

End Sub

Other Flags

Some methods of the Common Dialog box use other flags to control behaviors. The Font dialog Flags property must be set to the value 1 to load the system fonts.

After the font dialog box appears and selections are made, the values the user selected are available to the program.

The psuedocode for the font subroutine is:

Set Cancel Error flag
Error handler
Set Flags property to 1
Display the dialog box
Set font properties for bold, italic, font strike through font size, font name, and properties of text box (example: txtFontBold = CommonDialog1.FontBold)

Error label (in this case, we don’t need ANY code here!)

A Shortcut

Rather than typing

txtFontBold = CommonDialog1.FontBold
txtFontItalic = CommonDialog1.FontItalic

etc,, we can use a shortcut

With txtDisplay.Font

.Bold = CommonDialog1.FontBold
.Italic = CommonDialog1.FontItalic
etc.

End With

The Code for the Font Dialog is

Private Sub cmdFont_Click()

CommonDialog1.CancelError = False
On Error GoTo FontError

With CommonDialog1

.FontName = txtWord.FontName
.Flags = 1
.ShowFont
txtWord.Font.Name = .FontName
txtWord.FontBold = .FontBold
txtWord.FontItalic = .FontItalic
txtWord.FontSize = .FontSize

End With

Exit Sub

FontError:

End Sub 

The Color Dialog

set CancelError to true
Error handler
display the color dialog box
set the text box property (either BackColor or ForeColor) to CommonDialog1.Color
Error routine: no code needed.

The Open File Dialog

When an Open File dialog box appears, the user is presented with a selection of file types to choose from, with one type being the default (like *.doc files in Word). This is controlled by the .Filter and .FilterIndex properties. The .Filter describes the file types in a drop down menu.

note: the Filter begins and ends with a quote marks. The pipe | is used as a separator.

these pipes come RIGHT AFTER the file type (no space)

CommonDialog1.Filter = "Text Files (*.txt) | *.txt| All Files (*.*) |*.*|HTML Files (*.htm)|*.htm*"

to make the first item -"Text Files (*.txt) the default, use the FilterIndex Property

CommonDialog1.FilterIndex = 1

The code for the Open File dialog is:

On Error GoTo OpenError
Dim F As Integer
F = FreeFile
CommonDialog1.CancelError = True
CommonDialog1.Filter = "Text Files (*.txt)|*.txt| Web Files (*.htm)|*.htm"
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As F
txtWord.Text = Input(LOF(F), F)
Close #F

Exit Sub

OpenError:

Files

The values returned from the dialog box are used to open a file. Computers do not write or read directly to files, they use buffers. A buffer number and a file name must be obtained, then the file can be assigned to a buffer and used. The psuedocode looks like this:

Set Cancel Error to False
Error handler
Declare integer variable (F) for buffer number

With CommonDialog1

set the filter
set the Filter index = 1
display Open common dialog
set FileName to .FileName

End With

Assign buffer
Open the file (FileName) For Input
Read in the whole file Input(LOF(F), F to the text box
Close the file

Exit Sub

OpenError:

The Print Dialog

To display the Print Dialog, use

CommonDialog1.ShowPrinter

Some of the properties returned are:

FromPage

start page selected

ToPage

last page selected

Copies

number of copies

Printing is easy. Visual Basic considers the printer to be an object, and uses the object's Print method.

Printer.Print <what you want printed>
Printer.EndDoc

Thus to print the contents of a text box, use

Printer.Print txtWord.Text
Printer.EndDoc

Of course, you can use the properties returned by the Printer Dialog to control how the printer works. For example, you can use the Copies properties in a For loop to have the page printer more than once.

Display print dialog box
For i = 1 to CommonDialog1.Copies

Printer.Print txtWP.Text
Printer.EndDoc

Next i   

Saving A File

To save a file, use the Print# command with the buffer number.

Print #F, txtWord.Text

Before you do this, you must:

Set Cancel Error
Error Handler
Dim F for buffer number

set the filter to "Text Files (*.txt) | *.txt| All Files (*.*) |*.*|HTML Files (*.htm)|*.htm*"
set the filter index to 1
show the save box
set file name to dialog box file name

End With
Open the file for output
Print the text box to the file (Print #F, txtWord.Text)
Close the fileF

Save or Save As?

If the user chooses Save As, they should always be shown the Save common dialog box. However, if the choose Save, the computer must make a decision.

If (the file has a name)

save using the filename

else

show the Save dialog

End If

Because the Save dialog must always be shown when the user chooses SaveAs, we will actually be adding the dialog box  mnuSaveAs rather that mnuSave.

How do we know if the file has a name?To do this, we will create a global string variable called FileName. We will set this name to "nothing" in Form_Load. In our mnuSaveAs subroutine, we will assign FileName the name of the file being saved. In mnuOpen, we must also assign the opened file name to FileName. In mnuNew, we will set it back to "nothing." (Remember, in programming "nothing" is indicated by using two quote marks with NOTHING between them - not even a space!) In sum,

bullet On Form_Load - Set FileName to ""
bullet mnuSaveAs - Set FileName to CommonDialog1.FileName
bullet mnuOpen - Set FileName to CommonDialog1.FileName
bullet mnuNew - Set FileName to ""

These lines must be added to the appropriate subroutines.

The psuedocode needed to save a file are:

Set CancelError to False
Error Handler
Dim F As Integer 'this is the buffer number
F = FreeFile
If (the file has a name) 'just save it with that name

Open FileName For Output As F
Print #F, txtWord.Text
Close F

Else

mnuSaveAs_Click 'call mnuSaveAs

End If

Save As

CommonDialog1.CancelError = False
On Error GoTo SaveError
Dim F As Integer 'this is the buffer number
F = FreeFile
With CommonDialog1

.Filter = "Text Files (*.txt) | *.txt| All Files (*.*) |*.*|HTML Files (*.htm)|*.htm*"
.FilterIndex = 1
.ShowSave
FileName = .FileName

End With
Open FileName For Output As F
Print #F, txtWord.Text
Close F


Exit Sub

You should now be able to add save, save as, open, print, and set font to your word processor!