How-To... - Use The Common Dialog Control

Why bother writing and designing your own colour or font picker, open, save and print box when it is already there for you. Visual Basic already allows you to use common dialog boxes which are used by hundreds of applications, included Windows. To follow this How-To you need to make sure the Common Dialog control is in your toolbox.

 

If it is not, press CTRL+T or click the Tools menu and choose Custom Controls while running Visual Basic. In this box, click Browse and choose ComDLG32.ocx which is in the Windows System. Click Ok in the Custom Control box and the Common Dialog Control will be added to your toolbox.

Setting up the form

Create a form(with the Name of frmMain, similar to above, with 5 buttons(cmdFont, cmdColour, cmdPrint, cmdOpen, cmdSave) and a label(lblText).

Add the Common Dialog control and give it the name of CommonDialog. Change its Cancel property to True. Give the Flags property a value of 3.

Add an image control(not shown above) and give it the name of imgLoad.

Adding the code

Below is the full code for the above program. Paste the whole code into the General Declerations part of the Form. Each line of the code is full documented.

'--------------------------------------
'How-To Use the Common Dialog Control
'by David Cowan(Cowan-Creations)
'--------------------------------------
'
'This How-To shows you how to use the
'Common Dialog control to open and save a
'file, change fonts and colours and print
'
'This How-To only goes into the basics of
'the Common Dialog control, with the slightest
'of error handling.
'
'VB How-To
'www.cowan-creations.co.uk/vbhowto/
'
'E-mail : vbhowto@cowan-creations.co.uk

Private Sub Common_Error()
'---------------------------------
'This is used to catch the error
'that occurs when Cancel is clicked,
'otherwise the problem will crash.
'---------------------------------

'If error is 32755("Cancel was clicked") then
If Err.Number = 32755 Then
    'Do nothing because
    'nothing is required...
End If

End Sub

Private Sub cmdColour_Click()
'-------------------------------------
'The Colour command button is clicked.
'-------------------------------------

'If an error occurs goto ErrorH(below)...
On Error GoTo ErrorH

'Show the Colour commondialog box...
CommonDialog.ShowColor

'The line below is run AFTER Ok is clicked on the
'Colour box.
'The line changes the forms background colour
'to the colour that the user picks...
frmMain.BackColor = CommonDialog.Color

'If an error occurs(usually when Cancel is clicked)
ErrorH:
Common_Error 'Call the Common_Error sub-routine...
Exit Sub     'Exit the sub(if an error)...

End Sub

Private Sub cmdFont_Click()
'-------------------------------------
'The Font command button is clicked.
'-------------------------------------

'If an error occurs goto ErrorH(below)...
On Error GoTo ErrorH

'Show the Colour commondialog box...
CommonDialog.ShowFont

'The line below is run AFTER Ok is clicked on the
'Font box.
'The line changes the labels font to the
'Font picked by the user...
lblText.Font = CommonDialog.FontName

'If an error occurs(usually when Cancel is clicked)
ErrorH:
Common_Error 'Call the Common_Error sub-routine...
Exit Sub     'Exit the sub(if an error)...

End Sub

Private Sub
cmdOpen_Click() '------------------------------------- 'The Open command button is clicked. '------------------------------------- 'If an error occurs goto ErrorH(below)... On Error GoTo ErrorH 'This setups the dialog box to only 'allow Icons to be loaded... CommonDialog.Filter = "Icons|*.ico" 'The Open dialog box is shown... CommonDialog.ShowOpen 'The line below is run AFTER Ok is clicked on the 'Open box. 'The chosen picture is loaded into the image control... imgLoad.Picture = LoadPicture(CommonDialog.filename) 'If an error occurs(usually when Cancel is clicked) ErrorH: Common_Error 'Call the Common_Error sub-routine... Exit Sub 'Exit the sub(if an error)... End Sub Private Sub cmdPrint_Click() '------------------------------------- 'The Print command button is clicked. '------------------------------------- 'If an error occurs goto ErrorH(below)... On Error GoTo ErrorH 'The Print button will only display the box and not print... MsgBox "This will not print anything to the printer, it is just used for an example" 'Show the Print dialog box... CommonDialog.ShowPrinter 'If an error occurs(usually when Cancel is clicked) ErrorH: Common_Error 'Call the Common_Error sub-routine... Exit Sub 'Exit the sub(if an error)... End Sub Private Sub cmdSave_Click() '------------------------------------- 'The Save command button is clicked. '------------------------------------- 'If an error occurs goto ErrorH(below)... On Error GoTo ErrorH 'The Save dialog box is shown... CommonDialog.ShowSave 'The line below is run AFTER the OK button is clicked. 'The image in the image control is saved to the 'location the user picked... SavePicture imgLoad.Picture, CommonDialog.filename 'If an error occurs(usually when Cancel is clicked) ErrorH: Common_Error 'Call the Common_Error sub-routine... Exit Sub 'Exit this sub(if an error)... End Sub

You can download the source code by clicking here


This site is Copyright © 1999 of David Cowan. VB How-To, Cowan Creations, Dodgesoft and DodgeSoft Productions is Copyright © 1999 of David Cowan. Source-code and programs are copyright of their respective owners. For a messageboard, click here