Common Dialog Boxes


    You can use a set of predefined standard dialog boxes is your projects for such tasks as specifying colours and fonts, printing, opening, and saving. The common dialog control, which is a common control, allows your project to use the dialog boxes that are provided as part of the Windows environment.

    To use the common dialog control, you first place a control on your form. You only need one control and its location does not matter as it will be invisible when your program runs. Note: Windows determines the size of the dialog boxes; you cannot modify their size.

    The common dialog control may not appear in your toolbox. It is a custom control, and you must add it to Visual Basic before you can use it. Custom controls are stored in files associated with an extension of .OCX. Open the Project menu and choose Components (or press CTRL + T). Make sure that a check appears next to the item, and its tool should appear in your toolbox.

 

        When you name a common dialog control use "dlg" as its three character prefix.

Using a Common Dialog Box

    Once you have placed a common dialog control on your form, you can display any of its dialog boxes at run time. In code you specify which box you want with the Show method.

Object.ShowMethod

    The method can be one of the following:

Dialog Box Method
Open ShowOpen
Save ShowSave
Color ShowColor
Font ShowFont
Print ShowPrint

    For example, assume you have a common dialog control called dlgCommon and a menu item named mnuEditColor. You could display the Color dialog box in the Click event for the menu command.

Private Sub mnuEditColor_Click ( )
   ' Display the Color Dialog Box

    dlgCommon.ShowColor        ' Display the Color dialog box
End Sub

Using the Information from the Dialog Box

    Just because you displayed the Color dialog box doesn't make the colour of anything change. You must take care of that in your program code. What does happen is that the user's choices are stored in properties that you can access. You can assign these values to the properties of controls in your project.

Color Dialog Box

    The colour selected by the user is stored in the Color property. You can assign this property to another object, such as a control or to the form.

frmMain.BackColor = dlgCommon.Color

    Since Basic executes the statements in sequence, you would first display the dialog box with the ShowColor mehtod. (Execution then halts until the user responds to the dialog box.) Then you can use the Color property.

Private Sub mnuEditColor_Click ( )
   ' Display the Color Dialog Box

    dlgCommon.ShowColor        ' Display the Color dialog box   
    ' Asssign dialog box color to the form
   
frmMain.BackColor = dlgCommon.Color 
End Sub