|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Getting The File Name In Open, Save As DialogsNow that youve used the Common Dialog controls ShowOpen or ShowSave to display an Open or Save As dialog box, how do you get the file name the user has specified? You do that using one of two properties after the user clicks on the OK button:
Heres an example where weve set a Common Dialog controls CancelError property to True so Visual Basic will create a trappable cdlCancel error if the user clicks the Cancel button, show a File Open dialog box, and display the name and path of the file the user selected in a message box:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub
You can set the Filter property so the dialog box displays only certain types of files, such as text files. The Flags property can be used to change various elements on the dialog box, as well as to prompt the user when certain actions may occur, such as overwriting a file. See Setting File Types (Filters) In Open, Save As Dialogs for more on filters. For more on flags, 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 Maximum File Name Size In Open And Save As Dialog BoxesYou can use the Common Dialog controls MaxFileSize property to setnot the maximum file size you can open, but the maximum file name size. You set this property to a number of bytes as follows, where were restricting the file name and path to fit into 100 bytes: CommonDialog1.MaxFileSize = 100 This is useful if youre passing file names to other programs that cant use names longer than a certain length.
Setting Default File ExtensionsLike many Windows programs, you can make your programs set the default extension for the types of files you want to save (for example, .txt) if the user doesnt specify one. You specify a default extension with the Common Dialog controls DefaultExt property. An example will make this clearer. Here, we set the default extension of our files to save to txt by setting the DefaultExt property:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.DefaultExt = "txt"
CommonDialog1.ShowSave
MsgBox "File to save: " & CommonDialog1.FileName
Cancel:
End Sub
Lets say the user just types a file name without an extension, such as phonebook, in the Save As dialog box; the dialog box will then report the actual name of the file to save as phonebook.txt. If, on the other hand, the user specifies a file extension, that extension is preserved. Set Or Get The Initial DirectoryThe Testing Department is calling again: users of your program, SuperDuperTextPro, are complaining. When they want to save many files to their favorite directory, C:\poetry\roses\are\red\violets\are\blue, they have to open folder after folder each time to get back to that directory. Cant you let them set a default directory to save files to? You can, using the Common Dialog controls InitDir property. For example, heres how we set the initial directory to C:\windows when we open files using the Open dialog box:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.InitDir = "c:\windows"
CommonDialog1.ShowOpen
MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub
Running this code results in the Open dialog box you see in Figure 11.7.
Setting the initial directory like this can make multiple opens or saves much easier, which is very considerate to the user (and I know of some Microsoft software that could benefit by doing this). Setting File Types (Filters) In Open, Save As DialogsThe Testing Department is calling again. Your program, SuperDuperGraphics4U, only works with graphics files, but somehow users are trying to open text (.txt) filesand crashing the program. Is there some way you can clue them in as to allowed file types when they open files? Yesyou can set the Common Dialog controls Filter property to indicate the allowed file types and extensions in a drop-down list box in the Open and Save As dialog boxes. (To see an example of such a drop-down list box, use Visual Basics Save Project As menu item in the File menu; this list box gives two file extension types: *.vbp and all files, *.*.) To set up the Filter string, you separate prompts to the userfor example, Text files (*.txt)with upright characters (|, also called the pipe symbol) from the file specifications to Visual Basic (*.txt). (Dont add extra spaces around the uprights, because if you do, theyll be displayed along with the rest of the file extension information.) This is obviously one of those things made easier with an example (in fact, I always forget how to set up file filter strings unless I can work from an example), so lets see one now. Here, well let the user select from three options: text files (*.txt), image files (*.jpg, *.gif), and all files (*.*). We set the Filter string this way in that case; look closely at the following string and youll be able to see how to set up this string for yourself. (Here weve also set the Common Dialog controls CancelError property to True to create a trappable error if the user clicks the Cancel button):
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Filter = "Text files (*.txt)|*.txt|Image files _
(*.jpg, *.gif)|*.jpg;*.gif|All files (*.*)|*.*"
CommonDialog1.ShowOpen
MsgBox "File to open: & CommonDialog1.FileName
Cancel:
End Sub
Note in particular that when you have two file extensions for one file typeas we do for image files (*.jpg, *.gif)you surround the file extensions with a semicolon (;) and enclose them in parentheses. The result of this code appears in Figure 11.8. Here, were letting the user select from our three types of files: text files (*.txt), image files (*.jpg, *.gif), and all files (*.*).
|
|
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. |