Beginning
Overview
IDE
Projects
Forms
Controls
Intrinsic Ctrls
ActiveX Ctrls
Code
Commands
Cmd Reference
File Handling
Printing
Distribution

Intermediate
Menus
Dialogs
Mouse
Drag and Drop
Graphics
Error Handling
Databases
Database Controls
SQL

Advanced
API
Objects
Internet File Transfers
UNIX/CGI/Perl
Perl Commands
JavaScript
Web Page Forms

GBIC >> VB >> Tutorials >> Common Dialogs
Common Dialogs
The whole point of a visual language like VB is that it makes certain parts of programming (usually the user interface) much easier on the programmer. The idea is to build in features which programmers use over and over again. In the case of common dialogs I'm happy to say that Microsoft has really done us a good turn. I've written my own dialog windows (windows which provide information our request inputs from users) and I've used the built-in common dialog windows that VB provides. It's a no-brainer -- use the common dialog unless there's no other way!


Common Dialogs Mean Less Code!

In my beginner's section I state that a programmer's job is to get to the customer results as quickly and efficiently as possible. Common Dialogs are a great way to abide by this strategy.

VB provides a single .OCX file which contains the code for all 5 of the windows known as the common dialogs. Without further ado, here are the five:

  • Open
  • Save As
  • Color
  • Font
  • Print

Before we get into using each one, remember that the common dialog windows do not do anything automatically. All they do is provide a way of asking your user some questions and returning the answers to your application. You must write the code to use the answers!

For example, if your application displays the Open common dialog and your user selects a file name to open, absolutely nothing will happen. You have to write the code that peforms the open functions. The Open common dialog box only provides an easy way for you to ask the user which file he wants to open.

You could write a completely custom version of the Open common dialog, customizing it to your specific needs. However, this can take considerable time and introduces code which you must maintain. It's far better to use the proven common dialog window than to invent something new every time!

Calling Up the Common Dialog Windows
To use the common dialogs you must first add the Common Dialog OCX to your toolbox. This is done by right-mouse clicking on the toolbox, selecting Components, and the checking the Microsoft Common Dialog Control 6.0. Select OK for the selection to take effect.

The code for displaying a common dialog box couldn't be simpler. Simply use code to invoke the appropriate Method of the common dialog object as follows:

  • CommonDialog1.ShowOpen

The common dialog object also supports the .ShowSave, the .ShowColor, the .ShowFont and the .ShowPrinter methods which cause the display of the corresponding common dialog window.

A few other points are in order. If you look at my Control Summary Chart you'll see that the common dialog control has properties which you can set in code before you display the dialog windows. In this tutorial I don't discuss all of the possible settings. However both the VB HELP files and the VB 6.0 Programmer's Guide provide sample code.

Be sure to take note of the fact that by default, when a user selects "Cancel" in a common dialog window that a program error will occur. If you don't override this by setting the .CancelError property, you'll have to include error detection code in the procedure which invokes the display of the common dialog window.

Also take note that the common dialog control allows you to control the display of various aspects of the window by setting the .Flags property. You really should read through the HELP file's discussion of all of the options you can control using the .Flags property. There's a lot of power in that one property and you can add some powerful customization of your dialog windows if you use them fully.

Open Common Dialog Window

Save As Common Dialog Window

Color Common Dialog Window

Font Common Dialog Window

Print Common Dialog Window

And last but not least, a common error seen when using the Font dialog window is due to not setting the .Flags property. That property must be set (check HELP for allowed settings) before the Fonts dialog window will display correctly. You'd think Microsoft would have given it a default value, but as it is you must set the property!