|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Now that youve 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.
Setting A Common Dialogs TitleThe Aesthetic Design Department is calling again: cant 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 Dialogs title, you can do it using the DialogTitle property. As an example, here were changing the title of an Open dialog box to Select a file to open (see Figure 11.3):
Private Sub Command1_Click()
CommonDialog1.DialogTitle = "Select a file to open"
CommonDialog1.ShowOpen
End Sub
Did The User Click OK Or Cancel?Youve 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 controlfor 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 youve 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
Using A Color Dialog BoxThe Aesthetic Design Department is calling again. Wouldnt 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 boxs Color property. There are special flags you can set for the Color dialog boxsee the next topic for more information. Lets see an example. When the user clicks a button, well 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 controls 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 boxs 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
Thats itwhen you run the program and click the button, the Color dialog box appears, as in Figure 11.4.
When the user selects a color and clicks on OK, the program sets the text boxs background color to the newly selected color, as shown in Figure 11.5.
Now were using the Color dialog box. The listing for the preceding program is located in the colordialog folder on this books accompanying CD-ROM. Setting Color Dialog FlagsThere 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:
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 were doing numerically, its usually better to use constants to make the code more readable.) Adding the desired constant values produces the same result.
|
|
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. |