Common Dialog Control

Tips >> Visual Basic


The Common Dialog Control provides a standard interface for operations such as opening, saving, and printing files or selecting colours and fonts using the Microsoft Windows dynamic link library COMMDLG.DLL. The Control is visible on the form as an icon at design-time but not at run-time. The icon cannot be resized on the form and it's not possible to specify where the Dialog will be displayed when opened. To use a Common Dialog Control, add "Microsoft Common Dialog Control" from the "Project", "Components" menu item. The three-letter mnemonic for a Common Dialog Control is "cdl". The Control's Methods are used to determine which dialog is displayed.


The ShowColor method is used to display the Colour dialog box.

The Color property is used to determine which colour was selected.

picPallette.FillColor = cdlColour.Color


Alternatively, you may wish to break out the long number into its "Red", "Green" and "Blue" parts.

Dim Red As Long, Green As Long, Blue As Long
Red = cdlColour.Color And &HFF
Green = (cdlColour.Color \ &H100) And &HFF
Blue = cdlColour.Color \ &H10000




The ShowFont method is used to display a list of fonts that are available. Before the ShowFont method can be displayed, the Flags property must be set to one of the following constants or a Message Box is displayed with a message of "There are no fonts installed", and a run-time error occurs.

  • cdlCFScreenFonts
  • cdlCFPrinterFonts
  • cdlCFBoth
' Set default values for Font
cdlFont.FontName = lblSurname.FontName
cdlFont.FontSize = lblSurname.FontSize
cdlFont.Flags = cdlCFScreenFonts
lblSurname.FontName = cdlFont.FontName
lblSurname.FontSize = cdlFont.FontSize




The ShowHelp method is used to display a Windows Help File. You can write your own help files using the "Help Workshop" and display them with the Common Dialog Control's ShowHelp method. The HelpFile and HelpCommand properties must be set in order for the Help file to be displayed.

Help files are compiled files that are run using Winhlp32.exe. The Help Workshop that is part of the Visual Studio suite can be used to create Help files. The topics are written as Rich Text Format .rtf files. These are linked with other resources such as bitmaps into a single help file. The Help Workshop enables you to edit the project and contents files, and compile and test the help files.

The contents file is an ASCII text file, extension .cnt, that provides the instructions for the Contents tab in the Help Topics dialog box, and directs WinHelp to display the keywords of specified Help files on the Index and Find tabs. The contents file specifies the Headings, Topics, and Commands.

Headings contain a group of related topics and other Headings and is shown as a Book Icon. Topics display a topic of execute a macro and is shown as a page icon. The Commands specify the name of the default help file, title, the names of help files to be included on the Find tab and the names of help files containing keywords for KLink and ALink macros.

cdlHelp.HelpFile = "c:\juicystudio\tutorial.hlp"
cdlHelp.HelpCommand = cdlHelpContents

The ShowOpen method is used to display the dialog to open files. The FileName Property is used to determine the name of the file the user selected.

The Filter Property is used to determine the types of file to be shown. A Pipe symbol, |, is used to separate the description and value.
cdlOpen.Filter = "Text (*.txt)|*.txt"


Further filters may be specified on the same line separated by the Pipe symbol.

The following example uses a Common Dialog Control to allow either Text or Picture files to be selected.

Dim pattern As String
pattern = "Text (*.txt)|*.txt"
pattern = pattern & "|Pictures (*.bmp;*.gif;*.jpg)"
pattern = pattern & "|*.bmp;*.gif;*.jpg"
cdlOpen.Filter = pattern
picViewer.Picture = LoadPicture(cdlOpen.FileName)




The ShowPrinter method is used to display the dialog to open files.

The following example uses the ShowPrinter method with the Printer object to set the number of copies and page orientation.

Printer.Copies = cdlPrinter.Copies
Printer.Orientation = cdlPrinter.Orientation




The ShowSave method looks exactly the same as the ShowOpen method, except the default title is "Save As" rather than "Open".

The following example uses the ShowSave method with the FSO to write the URL to a text file specified by the user.

Dim fso As New FileSystemObject
Dim txtStream As TextStream
Set txtStream = fso.CreateTextFile(cdlSave.fileName, True)
txtStream.WriteLine ""
Set txtStream = Nothing
Set fso = Nothing

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.
My privilage card software   IDBL bank software   Himalayan software   ICCI Foundation software   Camlin software   OKAYA software   Microtech software   Action tesa software   Wonderchef software   Vichare courier software   Bonanza software   Globtech group software   Mascot universal software   Connel Brds software   GDA software   Asian software   Roger car technology software   Elite International software   Charminar software   Brave software   Goldmedal software   Slider software   National College software   Mafatlal software   Krishgen BioSystems software  
E-mail :
Phone : +91 022 28067978