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


Getting The File Name In Open, Save As Dialogs

Now that you’ve used the Common Dialog control’s 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:

  FileName—Holds the file name the user selected, with the file’s full path.
  FileTitle—Holds just the file’s name, without the path.

Here’s an example where we’ve set a Common Dialog control’s 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 Boxes

You can use the Common Dialog control’s MaxFileSize property to set—not 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 we’re restricting the file name and path to fit into 100 bytes:

CommonDialog1.MaxFileSize = 100

This is useful if you’re passing file names to other programs that can’t use names longer than a certain length.


TIP:  When using the cdlOFNAllowMultiselect flag, you may want to increase the value in the MaxFileSize property to allow enough memory for the selected file names.

Setting Default File Extensions

Like 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 doesn’t specify one. You specify a default extension with the Common Dialog control’s 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

Let’s 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 Directory

The 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. Can’t you let them set a default directory to save files to?

You can, using the Common Dialog control’s InitDir property. For example, here’s 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.


Figure 11.7  Setting an initial directory.

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 Dialogs

The Testing Department is calling again. Your program, SuperDuperGraphics4U, only works with graphics files, but somehow users are trying to open text (.txt) files—and crashing the program. Is there some way you can clue them in as to allowed file types when they open files?

Yes—you can set the Common Dialog control’s 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 Basic’s 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 user—for example, “Text files (*.txt)”—with upright characters (“|”, also called the pipe symbol) from the file specifications to Visual Basic (“*.txt”). (Don’t add extra spaces around the uprights, because if you do, they’ll 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 let’s see one now. Here, we’ll 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 you’ll be able to see how to set up this string for yourself. (Here we’ve also set the Common Dialog control’s 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 type—as 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, we’re letting the user select from our three types of files: text files (*.txt), image files (*.jpg, *.gif), and all files (*.*).


Figure 11.8  Setting file extension types in dialog boxes.


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.