Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

112 Part I Getting Started with Microsoft Visual Basic 2010


Event Procedures That Manage Common


Dialog Boxes


After you create a dialog box object, you can use the dialog box in a program by doing the
following:

n If necessary, set one or more dialog box properties by using program code before
opening the dialog box.
n To open the dialog box, type the dialog box name with the ShowDialog method in
an event procedure associated with a toolbar button or menu command.
n Use program code to respond to the user’s dialog box selections after the dialog box
has been manipulated and closed.
In the following exercise, you’ll enter the program code for the OpenToolStripButton_Click
event procedure, the routine that executes when the Open command is clicked. You’ll set
the Filter property in the OpenFileDialog1 object to define the file type in the Open common
dialog box. (You’ll specify Windows bitmaps .) Then you’ll use the ShowDialog method
to display the Open dialog box. After the user has selected a file and closed this dialog
box, you’ll display the file he or she selected in a picture box by setting the Image property
of the picture box object to the file name the user selected.

Edit the Open button event procedure


  1. Double-click the Open button on your form’s toolbar.


The OpenToolStripButton_Click event procedure appears in the Code Editor.


  1. Type the following program statements in the event procedure. Be sure to type each
    line exactly as it’s printed here, and press the ENTER key after each line.


OpenFileDialog1.Filter = "Bitmaps (*.bmp)|*.bmp"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = System.Drawing.Image.FromFile _
(OpenFileDialog1.FileName)
End If
The first three statements in the event procedure refer to three different properties of
the open file dialog box object. The first statement uses the Filter property to define
a list of valid files. (In this case, the list has only one item: * .bmp .) This is important for
the Open dialog box because a picture box object can display a number of file types,
including:
o Bitmaps ( .bmp files)
o Windows metafiles ( .wmf files)
o Icons ( .ico files)
Free download pdf