Event Procedures That Manage Common Dialog Boxes


Event Procedures That Manage Common Dialog Boxes

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

  • Type the dialog box name with the ShowDialog method in an event procedure associated with a toolbar button or menu command.

  • If necessary, set one or more dialog box properties by using program code before opening the dialog box.

  • 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.

  2. Type the following program statements in the event procedure. Be sure to type each line exactly as it's printed here, and press the Down Arrow key after the last 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 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:

  • Bitmaps (.bmp files)

  • Windows metafiles (.emf and .wmf files)

  • Icons (.ico files)

  • Joint Photographic Experts Group format (.jpg and .jpeg files)

  • Portable Network Graphics format (.png files)

  • Graphics Interchange Format (.gif files)

To add additional items to the Filter list, you can type a pipe symbol (|) between items. For example, this program statement

OpenFileDialog1.Filter = "Bitmaps (*.bmp)|*.bmp|Metafiles (*.wmf)|*.wmf"

allows both bitmaps and Windows metafiles to be chosen in the Open dialog box.

The second statement in the event procedure displays the Open dialog box in the program. ShowDialog is similar to the Show method in Visual Basic 6, but it can be used with any Windows form. The ShowDialog method returns a result named DialogResult, which indicates the button on the dialog box that the user clicked. To determine whether the user clicked the Open button, an If…Then decision structure is used to check whether the returned result equals DialogResult.OK. If it does, a valid .bmp file path should be stored in the FileName property of the open file dialog object. (You'll learn more about the syntax of If…Then decision structures in Chapter 6, “Using Decision Structures.”)

The third statement uses the file name selected in the dialog box by the user. When the user selects a drive, folder, and file name and then clicks Open, the complete path is passed to the program through the OpenFileDialog1.FileName property. The System.Drawing.Image.FromFile method, which loads electronic artwork, is then used to copy the specified Windows bitmap into the picture box object. (I broke this statement with the line continuation character (_) because it was rather long.)

Now you'll write an event procedure for the Color button that you added to the toolbar.

Write the Color button event procedure

  1. Display the form again, and then double-click the Color button on the toolbar that you added to the form.

    An event procedure named ToolStripButton1_Click appears in the Code Editor. The ob-ject name includes Button1 because it was the first non-standard button that you added to the toolbar. (You can change the name of this object to something more intuitive, such as colorToolStripButton, by clicking the button on the form and changing the Name property in the Properties window.)

  2. Type the following program statements in the event procedure:

    ColorDialog1.ShowDialog()  Label1.ForeColor = ColorDialog1.Color

    The first program statement uses the ShowDialog method to open the color dialog box. As you learned earlier in this chapter, ShowDialog is the method you use to open any form as a dialog box, including a form created by one of the standard dialog box controls that Visual Studio provides. The second statement in the event procedure assigns the color that the user selected in the dialog box to the ForeColor property of the Label1 object. You might remember Label1 from earlier in this chapter—it's the label box you used to display the current time and date on the form. You'll use the color returned from the color dialog box to set the color of the text in the label.

    Note that the Color dialog box can be used to set the color of any user interface element that supports color. Other possibilities include the background color of the form, the colors of shapes on the form, and the foreground and background colors of objects.

  3. Click the Save All button on the Standard toolbar to save your changes.

Controlling Color Choices by Setting Color Dialog Box Properties

If you want to further customize the color dialog box, you can control what color choices the dialog box presents to the user when the dialog box opens. You can adjust these color settings by using the Properties window, or by setting properties by using program code before you display the dialog box with the ShowDialog method. The following table describes the most useful properties of the ColorDialog control. Each property should be set with a value of True to enable the option or False to disable the option.

Property

Meaning

AllowFullOpen

Set to True to enable the Define Custom Colors button in the dialog box.

AnyColor

Set to True if the user can select any color shown in the dialog box.

FullOpen

Set to True if you want to display the Custom Colors area when the dialog box first opens.

ShowHelp

Set to True if you want to enable the Help button in the dialog box.

SolidColorOnly

Set to True if you want the user to select only solid colors (dithered colors—those that are made up of pixels of different colors—are disabled).

Now you'll run the Menu program and experiment with the menus and dialog boxes you've created.

Run the Menu program

TIP
The complete Menu program is located in the c:\vb05sbs\chap04\menu folder.

  1. Click the Start Debugging button on the Standard toolbar.

    The program runs, and the Clock menu and the toolbar appear at the top of the screen.

  2. On the form's toolbar, click Open.

    The Open dialog box appears. It looks great, doesn't it? Notice the Bitmaps (*.bmp) entry in the Files Of Type box. You defined this entry with the statement

    OpenFileDialog1.Filter = "Bitmaps (*.bmp)|*.bmp"

    in the openToolStripButton_Click event procedure. The first part of the text in quotes— Bitmaps (*.bmp)—specifies which items are listed in the Files Of Type box. The second part—*.bmp—specifies the file name extension of the files that are to be listed in the dialog box.

  3. Open the c:\windows folder (or another folder that contains bitmap images) on your hard disk, and scroll past the list of folders to see the bitmap files.

    A standard collection of bitmaps appears. Most of these files were included with the Windows operating system, and you might have added to the collection yourself.

    graphic

  4. Select one of the bitmap files, and then click the Open button.

    A picture of the bitmap appears in the picture box. (I've selected the Zapotec.bmp file.) Your form looks similar to this:

    graphic

    Now you'll practice using the Clock menu.

  5. On the Clock menu, click the Time command.

    The current time appears in the label box.

  6. Click the Color button on the toolbar.

    The Color dialog box appears, as shown here:

    graphic

    The Color dialog box contains elements that you can use to change the color of the clock text in your program. The current color setting, black, is selected.

  7. Click the blue box, and then click the OK button.

    The Color dialog box closes, and the color of the text in the clock label changes to blue, as shown below.

    graphic

  8. On the Clock menu, click the Date command.

    The current date is displayed in blue type. Now that the text color has been set in the label, it remains blue until the color is changed again or the program closes.

  9. Close the program.

    The application terminates, and the Visual Studio IDE appears.

That's it! You've learned several important commands and techniques for creating menus, toolbars, and dialog boxes in your programs. After you learn more about program code, you'll be able to put these skills to work in your own programs.

Adding Nonstandard Dialog Boxes to Programs

What if you need to add a dialog box to your program that isn't provided by one of the eight dialog box controls in Visual Studio? No problem—but you'll need to do a little extra design work. As you'll learn in future chapters, a Visual Basic program can use more than one form to receive and display information. To create nonstandard dialog boxes, you need to add new forms to your program, add input and output objects, and process the dialog box clicks in your program code. (These techniques will be discussed in Chapter 14, “Managing Windows Forms and Controls at Run time.”) In the next chapter, you'll learn how to use two handy dialog boxes that are specifically designed for receiving text input (InputBox) and displaying text output (MsgBox). These dialog boxes help bridge the gap between the dialog box controls and the dialog boxes that you need to create on your own.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net