Visual Basic: CommonDialog Control

ShowColor, ShowFont, ShowHelp, ShowOpen, ShowPrinter, ShowSave Methods Example

This example uses the CommonDialog control and the ShowColor, ShowFont, ShowHelp, ShowOpen, ShowPrinter, and ShowSave methods to display the common dialog boxes. To try this example, paste the code into the Declarations section of a form with CommandButton, OptionButton (set the option button's Index property to 0), and CommonDialog controls. Press F5 and select the option button for the common dialog box you want and choose the command button.

  Private Sub Form_Paint ()
   Static FlagFormPainted As Integer
   ' When form is painting for first time,
   If FlagFormPainted <> True Then
      For i = 1 To 5
         Load Option1(i)   ' Add five option buttons to array.
         Option1(i).Top = Option1(i - 1).Top + 350
         Option1(i).Visible = True
      Next i
      Option1(0).Caption = "Open"   ' Put caption on each option button.
      Option1(1).Caption = "Save"
      Option1(2).Caption = "Color"
      Option1(3).Caption = "Font"
       Option1(4).Caption = "Printer"
      Option1(5).Caption = "Help"
      Command1.Caption = "Show Dlg"   ' Label command button.
      FlagFormPainted = True   ' Form is done painting.
   End If
End Sub

Private Sub Command1_Click ()
   If Option1(0).Value Then   ' If Open option button selected,
      CommonDialog1.ShowOpen   ' display Open common dialog box.
   ElseIf Option1(1).Value Then   ' Or,
      CommonDialog1.ShowSave   ' display Save common dialog box.
   ElseIf Option1(2).Value Then   ' Or,
      CommonDialog1.ShowColor   ' display Color common dialog box.
   ElseIf Option1(3).Value Then   ' Or,
      CommonDialog1.Flags = cdlCFBoth   ' Flags property must be set
            ' to cdlCFBoth,                      ' cdlCFPrinterFonts, 
            ' or cdlCFScreenFonts before                ' using ShowFont method.
      CommonDialog1.ShowFont   ' Display Font common dialog box.
   ElseIf Option1(4).Value Then   ' Or,
      CommonDialog1.ShowPrinter   ' display Printer common dialog box.
   ElseIf Option1(5).Value Then   ' Or,
      CommonDialog1.HelpFile = "VB5.hlp"
      CommonDialog1.HelpCommand = cdlHelpContents
      CommonDialog1.ShowHelp   ' Display Visual Basic Help contents topic.
      End If
End Sub