VB6 FAQ for Common Dialog Control-VBForums
Results 1 to 2 of 2

Thread: VB6 FAQ for Common Dialog Control

  1. #1

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,920

    VB6 FAQ for Common Dialog Control

    This tutorial is about the Common Dialog Control (CDC) which provides a standard interface for operations such as opening, saving, printing, selecting colours/fonts and for showing Help using the Microsoft Windows dynamic link library COMMDLG.DLL.

    To use a Common Dialog Control, add "Microsoft Common Dialog Control" from the "Project", "Components" menu item. See picture below.




    This will add the control the the Tool Box as shown below.


    The best part is that CDC is visible on the form as a small icon at design-time but will be invisible at run-time.

    Like I mentioned above, the CDC can be used for the following operations.
    • Open File using .ShowOpen
    • Save File using .ShowSave
    • Setting Color using .ShowColor
    • Setting Font using .ShowFont
    • Print using .ShowPrinter
    • Display Help using .ShowHelp


    Lets cover each of them by creating a small Project.

    Setting up the form
    • Create a form and add 6 buttons and set their captions as shown in the picture below

    • Place the CDC in the form as shown above
    • And lastly put a Rich Text Box. (To use the Rich Text Box Control, add "Microsoft Rich TextBox Control" from the "Project", "Components" menu item.)


    Setting up the code

    Paste the code below and then simply run the form. I have commented the code so you won’t have any difficulty understanding it

    Code:
    '~~> ShowOpen Example
    Private Sub Command1_Click()
        On Error GoTo cancel_error
        
        With CommonDialog1
            '~~> Set the Title of the Dialog Box
            .DialogTitle = "Open"
            '~~> Set the Initial directory
            .InitDir = "C:\"
            '~~> Set the Filter for the files that you want to open
            .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            '~~> Displays the Open Dialog Box
            .ShowOpen
        End With
        
        '~~> Load the Rich Text Box with the text file
        RichTextBox1.LoadFile CommonDialog1.FileName
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    
    '~~> ShowSave Example
    Private Sub Command2_Click()
        On Error GoTo cancel_error
        
        With CommonDialog1
            '~~> Set the Title of the Dialog Box
            .DialogTitle = "Save"
            '~~> Set the Initial directory
            .InitDir = "C:\"
            '~~> Set the Filter for the files that you want to save as
            .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            '~~> Displays the Save Dialog Box
            .ShowSave
        End With
        
        '~~> Save the Rich Text Box contents as a text file
        RichTextBox1.SaveFile CommonDialog1.FileName
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    
    '~~> ShowColor Example
    Private Sub Command3_Click()
        On Error GoTo cancel_error
        
        With CommonDialog1
              .DialogTitle = "Color"
              '~~> Show the Color Dialog box
              .ShowColor
         End With
         
         '~~> Set the back color of the Dialogbox
         RichTextBox1.BackColor = CommonDialog1.Color
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    
    '~~> ShowFont Example
    Private Sub Command4_Click()
        On Error GoTo cancel_error
        
        With CommonDialog1
            .DialogTitle = "Font"
            
            '~~> Various flags that you can use
            ' cdlCFBoth         ~~> Display Screen's and Printer's fonts
            ' cdlCFEffects      ~~> For Underline, FontStrikethru, Color
            ' cdlCFTTOnly       ~~> Display True Type fonts
            ' cdlCFScreenFonts  ~~> Displays Screen fonts
            ' cdlCFPrinterFonts ~~> Displays Printer fonts
            
            .Flags = cdlCFBoth Or cdlCFEffects
            
            '~~> Show the Font Dialog box
            .ShowFont
        End With
        
        '~~> Select the entire text in the Rich Text Box
        '~~> Comment the below two lines if you want to
        '~~> Manually select words in the Rich Text Box
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = Len(RichTextBox1)
        
        '~~> Change Font
        RichTextBox1.SelFontName = CommonDialog1.FontName
        '~~> Change Font Size
        RichTextBox1.SelFontSize = CommonDialog1.FontSize
        '~~> Bold Font
        RichTextBox1.SelBold = CommonDialog1.FontBold
        '~~> Italicize Font if ticked
        RichTextBox1.SelItalic = CommonDialog1.FontItalic
        '~~> Underline if ticked
        RichTextBox1.SelUnderline = CommonDialog1.FontUnderline
        
        '~~> Strike through Font if ticked
        RichTextBox1.SelStrikeThru = CommonDialog1.FontStrikethru
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    
    '~~> ShowPrinter Example
    Private Sub Command5_Click()
        On Error GoTo cancel_error
        
        '~~> Show the printer dialog box
        CommonDialog1.ShowPrinter
        
        '~~> Print the text in the Rich Text Box
        Printer.Print RichTextBox1.Text
        Printer.EndDoc
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    
    '~~> ShowHelp Example
    Private Sub Command6_Click()
        On Error GoTo cancel_error
        
        '~~> Set the help file
        CommonDialog1.HelpFile = "MyHelp.hlp"
        CommonDialog1.HelpCommand = cdlHelpContents
        '~~> Show the Help File
        CommonDialog1.ShowHelp
    Exit Sub
    
    cancel_error:
         If Err.Number <> vbCancel Then
              MsgBox Err.Description
         End If
    End Sub
    I am also attaching the Zipped Project…

    Hope this helps…
    Attached Files Attached Files
    Last edited by Siddharth Rout; Jun 17th, 2010 at 12:43 PM. Reason: Typo
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  2. #2
    Addicted Member
    Join Date
    Jul 2010
    Posts
    146

    Re: VB6 FAQ for Common Dialog Control

    Since I just dealt with this issue myself, I thought I'd add a few things.

    If you want the Open/Save dialogs to look like they normally do in Windows programs and not like a Win 3.1 nightmare, you'll need to do the following:

    Const cdlOFNExplorer = &H80000

    then when setting up the dialog,

    .flags = cdlOFNExplorer


    Also, to open multiple files:
    Const cdlOFNAllowMultiselect = &H200

    .flags = cdlOFNExplorer Or cdlOFNAllowMultiselect

    (assuming you want the modern dialog too, if not, just the multiselect)

    The documentation states that multiple names in .Filename are space delimited; however this is not true. They are null-character delimited.
    So, to get at all the names, you'll need

    Split(.Filename, vbNullChar)

    The resulting array's first value (index 0) will be the path, without the filename, then 1 through ubound will be the names of files selected in that folder. Maybe it's just my system, but after opening multiple files and invoking the dialog again, the default name is set to a part of one of the previous file names, so if that happens just clear it by setting .Filename = "" before .Show... .

    For all the flags, see: http://msdn.microsoft.com/en-us/library/aa259317%28v=VS.60%29.aspx
    Last edited by fafalone; Jul 17th, 2010 at 10:17 PM. Reason: Clarified some things

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.