Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now that you’ve added a Common Dialog control to your program, refer to the individual topics in this chapter for the dialog box you want to work with to see how to retrieve values from the user.


TIP:  Before displaying the Font and Help dialog boxes, you need to set the Common Dialogs control’s Flags property or nothing will appear. See “Setting Color Dialog Flags,” “Setting Open and Save As Flags,” “Setting Font Dialog Flags,” and “Setting Print Dialog Flags” later in this chapter.

Setting A Common Dialog’s Title

The Aesthetic Design Department is calling again: can’t you change the text in the title bar of those dialog boxes? How about changing the title of the Open dialog box from “Open” to “Select A File To Open”?

Although some programmers may question the wisdom of changing a Common Dialog’s title, you can do it using the DialogTitle property. As an example, here we’re changing the title of an Open dialog box to “Select a file to open” (see Figure 11.3):


Figure 11.3  Our dialog box with revised title.

Private Sub Command1_Click()
    CommonDialog1.DialogTitle = "Select a file to open"
    CommonDialog1.ShowOpen
End Sub


WARNING!  Note that this property, DialogTitle, does not work for the Color, Font, and Print dialog boxes.

Did The User Click OK Or Cancel?

You’ve displayed your dialog box, and the user has dismissed it. But did the user click the OK or the Cancel button? Should you take action or not?

You can check which button the user has selected by examining the various properties of the dialog box control—for example, when the user clicks Cancel in a File Open dialog box, the FileName property returns an empty string, “”.

However, Visual Basic provides a more systematic way of checking which button was clicked. You can set the Common Dialog CancelError property to True to create a special, nonharmful, and trappable error, error number 32755 (Visual Basic constant cdlCancel), when the user clicks the Cancel button.

To trap this error if you’ve set the CancelError property to True, use On Error GoTo, and place the label control at the end of the procedure:

Private Sub Command1_Click()
    On Error GoTo Cancel
...
Cancel:
End Sub

Then you can show the dialog box and take action, assuming the user clicked on OK. If, on the other hand, the user clicked Cancel, control will go to the end of the procedure and exit harmlessly:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.ShowColor
    Text1.BackColor = CommonDialog1.Color
Cancel:
End Sub


TIP:  If you have enabled other trappable errors in your procedure (the On Error GoTo statement in the preceding code does not affect code outside the procedure it’s defined in), check to make sure that the error you’re expecting when the user clicks Cancel does in fact have the number cdlCancel. You can do this by checking the Err object’s Number property. Note also that Common Dialog controls can return errors besides cdlCancel—such as cdlHelp when the Help system failed to work properly, or cdlFonts if no fonts exist—and you might check for those separately.

Using A Color Dialog Box

The Aesthetic Design Department is calling again. Wouldn’t it be nice if you let the user select the color of the controls in your program? Yes, you say, but…. Great, they say, and hang up.

To let the user select colors, you use the Color dialog box, and you display that dialog box with the Common Dialog method ShowColor. To retrieve the color the user selected, you use the dialog box’s Color property. There are special flags you can set for the Color dialog box—see the next topic for more information.

Let’s see an example. When the user clicks a button, we’ll display a Color dialog box and let the user select the background color of a text box. Add a Common Dialog control to a form and set its CancelError property to True so that clicking Cancel will cause a cdlCancel error. Next, we trap that error this way:

Private Sub Command1_Click()
    On Error GoTo Cancel
...
Cancel:
End Sub

Now we use the Common Dialog control’s ShowColor method to show the color dialog:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.ShowColor
...
Cancel:
End Sub

When control returns from the dialog box, we use the Color property to set the text box’s background color to the color the user has selected:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.ShowColor
    Text1.BackColor = CommonDialog1.Color
Cancel:
End Sub

That’s it—when you run the program and click the button, the Color dialog box appears, as in Figure 11.4.


Figure 11.4  The Color dialog box.

When the user selects a color and clicks on OK, the program sets the text box’s background color to the newly selected color, as shown in Figure 11.5.


Figure 11.5  Setting a control’s color with the Color dialog box.

Now we’re using the Color dialog box. The listing for the preceding program is located in the colordialog folder on this book’s accompanying CD-ROM.

Setting Color Dialog Flags

There are a number of options you can set before displaying a Color dialog box, and you set them in the Flags property of the Common Dialog control. Here are the possible values:

  cdlCCRGBInit—1; sets the initial color value for the dialog box
  cdCClFullOpen—2; entire dialog box is displayed, including the Define Custom Colors section
  cdlCCPreventFullOpen—4; disables the Define Custom Colors command button and prevents the user from defining custom colors
  cdlCCHelpButton—8; causes the dialog box to display a Help button

You can set more than one flag for a dialog box using the Or operator. For example:

CommonDialog1.Flags = &H10& Or &H200&

(Note that although this shows what we’re doing numerically, it’s usually better to use constants to make the code more readable.) Adding the desired constant values produces the same result.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.