|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Using The Open And Save As DialogsProbably the most common use of the Common Dialog control is to display File Open and File Save As dialog boxes, and you display those dialog boxes with the Common Dialog controls ShowOpen and ShowSave methods. These methods need no arguments passed to themto set various options, you set the Common Dialog controls Flags property (see the next topic), such as overwriting existing files and so on. You can also set the Filter property so the dialog box displays only certain types of files, such as text files. See Setting File Types (Filters) In Open, Save As Dialogs a little later in this chapter. To find out what file the user wants to work with, you check the Common Dialogs FileName property after the user clicks on OK in the dialog box. That property holds the fully qualified (that is, with path) name of the file to open. If you just want the files name, use the FileTitle property. Lets see an example. In this case, well let the user select a file to open, and then display the files name and path in a message box. Start by adding a Common Dialog control to a form, then set the controls CancelError property to True so we can check if the user clicked Cancel. To check that, we use On Error GoTo:
Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub
Then we display the Open dialog box:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
...
Cancel:
End Sub
Finally, assuming the user clicked on OK, we can display the name of the file they selected in a message box using the FileName property:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub
When you run this code and click the button, the Open dialog box appears, as in Figure 11.6.
If you make a file selection and click on OK, the Open dialog box closes and the program displays the name of the file you selected, along with its path, in a message box. Our program is a success; the code for this program is located in the opendialog folder on this books accompanying CD-ROM. Setting Open And Save As FlagsYou can set a wide variety of options when you display File Open and File Save As dialog boxes by setting the Common Dialog controls Flags property. Here are the possible settings:
You can set more than one flag for a dialog box using the Or operator. For example: CommonDialog1.Flags = &H10& Or &H200& (Although this shows what were doing numerically, its usually better to use constants to make your 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. |