|
|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 11 Windows Common Dialogs
- If you need an immediate solution to:
- Creating And Displaying A Windows Common Dialog
- Setting A Common Dialogs Title
- Did The User Click OK Or Cancel?
- Using A Color Dialog Box
- Setting Color Dialog Flags
- Using The Open And Save As Dialogs
- Setting Open And Save As Flags
- Getting The File Name In Open, Save As Dialogs
- Setting Maximum File Name Size In Open And Save As Dialog Boxes
- Setting Default File Extensions
- Set Or Get The Initial Directory
- Setting File Types (Filters) In Open, Save As Dialogs
- Using A Font Dialog Box
- Setting Font Dialog Flags
- Setting Max And Min Font Sizes
- Using The Print Dialog Box
- Setting Print Dialog Flags
- Setting The Minimum And Maximum Pages To Print
- Setting Page Orientation
- Showing Windows Help From A Visual Basic Program
In Depth
In this chapter, were going to examine the Windows Common Dialogs, which provide a powerful and professional set of dialog boxes for interacting with the user. Microsoft created the Common Dialogs to promote a common user interface across all Windows programs, and in fact the Common Dialogs do work welland they make programming easier for the programmer. Having a common user interface across all Windows programs is valuable for the user, because it simplifies tasks. For the programmer, the Common Dialogs means that we have a powerful set of dialog boxes ready for us to use, without having to create them ourselves. From both ends of the spectrum, then, the Windows Common Dialogs may be considered a success.
The Common Dialog control can display five different dialog boxesOpen A File, Save A File, Set A Color, Set A Font, and Print A Document.
The Common Dialog Control
The Common Dialogs are all part of one control: the Common Dialog control. You add that control to a program with the Visual Basic Project|Components menu item. Click the Controls tab in the Components box that opens, and select the entry labeled Microsoft Common Dialog Control, then click on OK to close the Components box. You add a Common Dialog control to a form in the usual wayjust double-click the Common Dialog tool in the toolbox, or select it and paint the control on the form. The Common Dialog tool appears as the eleventh tool down on the right in the Visual Basic toolbox in Figure 11.1. The Common Dialog control will appear as a nonresizable icon on your form and is not visible at runtime.
Figure 11.1 The Common Dialog tool.
You use the controls Action property to display a dialog box or, equivalently, these methods:
- ShowOpenShow Open dialog box
- ShowSaveShow Save As dialog box
- ShowColorShow Color dialog box
- ShowFontShow Font dialog box
- ShowPrinterShow Print or Print Options dialog box
Besides these dialog boxes, you can also display Windows Help:
- ShowHelpInvokes the Windows Help engine
The Common Dialog control automatically provides context-sensitive Help on the interface of the dialog boxes. You invoke context-sensitive Help by clicking the Help button labeled Whats This in the title bar, then clicking the item for which you want more information. In addition, you can right-click the item for which you want more information, then select the Whats This command in the displayed context menu.
TIP: We might also note, by the way, that there is no way currently to specify where a dialog box is displayed; that might change in some future release.
As an example, the Font dialog box appears in Figure 11.2.
Figure 11.2 The Font dialog box.
Thats really all the overview we need. Were ready to start the Immediate Solutions now.
Immediate Solutions
Creating And Displaying A Windows Common Dialog
The Testing Department is calling again. Your program, SuperDuperTextPro, is great, but why is the File Save As dialog box the size of a postage stamp? And why is it colored purple? Shouldnt it match the uniform kind of dialog box that other Windows programs use?
To make your dialog boxes look just like the dialog boxes other programs use (and add professionalism to your program), you can use the Windows Common Dialogs, which are wrapped up in the Windows Common Dialog control. The Common Dialog control can display five different dialog boxesOpen A File, Save A File, Set A Color, Set A Font, and Print A Document, and you can also display Windows Help.
Adding a Windows Common Dialog control to your program is easy: just follow these steps:
- 1. Select the Project|Components menu item.
- 2. Select the Controls tab in the Components box that opens.
- 3. Select the entry labeled Microsoft Common Dialog Control, then click on OK to close the Components box.
- 4. Add a Common Dialog control to a form in the usual wayjust double-click the Common Dialog tool in the toolbox, or select it and paint the control on the form. (The Common Dialog tool appears as the eleventh tool down on the right in the Visual Basic toolbox in Figure 11.1.)
- 5. Add the code you want to open the dialog box and make use of values the user sets.
To display various dialog boxes, you use these Common Dialog methods (for example, CommonDialog1.ShowColor):
- ShowOpenShow Open dialog box
- ShowSaveShow Save As dialog box
- ShowColorShow Color dialog box
- ShowFontShow Font dialog box
- ShowPrinterShow Print or Print Options dialog box
- ShowHelpInvokes the Windows Help engine
You can also set the Common Dialogs Action property to do the same thing (and in fact, thats the way you used to display Common Dialogs until recent Visual Basic releases). Microsoft says that using the preceding methods adds functionality, but in fact, the two ways of displaying dialog boxes are equivalent at this writing (although using methods like ShowHelp instead of Action = 6 makes code a little clearer). Here are the values you can place in the Action property:
- 0No action
- 1Displays the Open dialog box
- 2Displays the Save As dialog box
- 3Displays the Color dialog box
- 4Displays the Font dialog box
- 5Displays the Print dialog box
- 6Runs winhelp32.exe
|