Quick Example: The Open Dialog Box

1. Start a new project. Place a common dialog control, a label box, and a command
button on the form. Set the following properties:
Form1:
Caption Common Dialog Examples
Name frmCommon
CommonDialog1:
DialogTitle Open Example
Filter Bitmaps (*.bmp)|*.bmp|
Icons (*.ico)|*.ico|Metafiles (*.wmf)|*.wmf
GIF Files (*.gif)|*.gif|JPEG Files (*,jpg)|*.jpg
(all on one line)
Name cdlExample
Label1:
BorderStyle 1-Fixed Single
Caption [Blank]
Name lblExample
Command1:
Caption &Display Box
Name cmdDisplay

When done, the form should look like this (make sure your label box is very long):

2. Attach this code to the cmdDisplay_Click procedure.
Private Sub cmdDisplay_Click()
cdlExample.ShowOpen
lblExample.Caption = cdlExample.filename
End Sub
This code brings up the Open dialog box when the button is clicked and shows
the file name selected by the user once it is closed.
3. Save the application. Run it and try selecting file names and typing file names.
Notice names can be selected by highlighting and clicking the OK button or just by
double-clicking the file name. In this example, clicking the Cancel button is not
trapped, so it has the same effect as clicking OK.
4. Notice once you select a file name, the next time you open the dialog box, that
selected name appears as default, since the FileName property is not affected in
code.
Image:

Name Image1

Stretch True

Border Style Fixed Single

Private Sub Command1_Click()
frmCommon.Show
Me.Hide
End Sub

Private Sub Form_Load()
Image1.Picture = LoadPicture(frmCommon.cdlExample.FileName)
End Sub