Web Development

Save time with VB6's Common Dialog control

VB6's Common Dialog control can save you a lot of programming time, as well as provide a standard Windows look to certain parts of your program. Learn how to use this control.

Many Windows programs have things in common, such as dialog boxes for opening files, selecting colors, or starting a print job. When you need this kind of functionality in your VB6 program, you may want to turn to the Common Dialog control instead of creating your own dialog box.

To use this control, you must select Microsoft Common Dialog Control 6.0 on the Controls tab in the Components window (select Project | Components from the menu). Then, the control icon will appear in your toolbox and you can add it as a form like any other control. At run time, it's invisible until you need it.

The Common Dialog control is very flexible—in fact, it's five dialog boxes in one. You choose the dialog box you need by the method you call on the control. Here are brief descriptions of the dialog boxes:

  • ShowOpen: Shows a File Open dialog box.
  • ShowSave: Shows a File Save dialog box.
  • ShowColor: Shows a Select Color dialog box.
  • ShowFont: Shows a Font Selection dialog box.
  • ShowPrinter: Shows a Print/Print Options dialog box.

The control has a range of properties, many of which are relevant only for a specific type of dialog box. For example, when using the ShowOpen method, you can set properties to specify the starting folder and the types of files that are shown. Likewise, the Color dialog box has a property that returns the color the user selects. A nice feature is that each dialog box automatically provides context-sensitive help on its elements.

Here's an example of using the File Open dialog box. Assume the control is named cdlg. The dialog box will initially show only *.txt files in the c:\temp folder:

cdlg.Filter = "Text (*.txt) | *.txt"
cdlg.InitDir = "c:\temp"
if cdlg.FileName = "" Then
    ' User canceled.
    ' The FileName property contains the selected file name.
End If

The Common Dialog control can save a lot of programming time, as well as provide a standard Windows look to certain parts of your program.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!


Editor's Picks