This lesson describes how to access files inside Visual Basic applications. The data stored in files is more
permanent than data stored in variables. The values of variables are too volatile. If you want to store a history of customer information, for
example, you must store that information on the disk. If you kept such information in arrays of variables, you
would never be able to exit Visual Basic or turn off the computer because you would lose all the information!
Data inside variables is needed for processing, calculating, printing, and storing, but not for long-term storage. Before you can access
data stored in files, you must be able to locate data stored on the disk. This unit teaches you how to build common
file-management dialog boxes that enable the user to select files stored on the disk. Depending on your application, you may need the user
to select a filename, and this unit's file dialog box discussion provides the tools that you'll need to display lists
of files from which the user can choose. After the user selects or enters a filename, you'll need to utilize the file I/O commands and
functions discussed in the next unit to access the data in the files.
Concept: There are several ways to obtain filename information from the user. The most common way is to build a dialog box that enables the user to select a file, directory, and drive from lists that you
provide.
Figure 17.1 shows a common File Add dialog box that you'll see if you select File Add from Visual Basic. You've seen this and similar File Open dialog boxes in more than one Windows application. The great thing about file dialog boxes is that the user
can select from a list of filenames, directories, and drives. Although you could ask the user for a filename using an input box, allowing the user to select from a list means that the user can often find files quickly and more accurately.
Figure 17.1. A dialog box that enables the user to select the filename, directory, and drive.
Dialog boxes are nothing more than secondary forms that you add to your application's primary Form window. The list
boxes, command buttons, and text boxes that you see on such dialog boxes are nothing more than controls that you place on those secondary
forms, just as you do on the primary Form window. We're going to have a small problem with the Visual Basic Primer
System, however, that comes with this book: The Visual Basic Primer doesn't allow for more than one form per application. It would seem at
first glance that we won't be able to add file-selecting dialog boxes to our applications.
Have no fear.
The frame control produces frames on which you can place file-selecting controls such as list boxes, command buttons, and text boxes. The frame control won't offer exactly the same kind of usability as a true dialog box because the
user can't
resize or move the frame. However, the frame does provide basically the same end result that allows us display file-selecting controls when needed.
Note: Technically, you can write a frame control with properties that enable the user to move the frame, but most of the time, the user shouldn't have that much power over controls. The user could easily hide other controls and mess up the application's design.
Figure 17.2 shows the file selection frame that you'll learn how to create and use in this unit. As you can see, the frame offers the same functionality as the dialog box shown in Figure 17.1. After the user
selects a file, you can program the frame to
disappear from view just as file-selection dialog boxes disappear.
Figure 17.2. A file-selection frame that mimics a file-selection dialog box.
Actually,
the frame file-selection dialog box is slightly easier to use than the standard file-selection dialog box because the frame combines the File Type box with the File Name text box. When the user enters a file pattern, such as *.frm in the File
Name text
box, the frame immediately updates the filename list box to reflect only files that match that pattern.
Stop and Type: Load and run the project named FILESEL.MAK that comes on one of this book's
companion disks (the disk that does not contain the Visual Basic Primer system). You'll see the application shown in
Figure 17.3. Select different drives and directories and notice how the file list updates to reflect the change. The rest of this unit
describes the important components of this application.
Review: Although you can't add additional forms to your Visual Basic Primer applications, you can simulate the additional forms by using the frame
control to hold sets of controls that mimic dialog boxes. This
unit devotes itself to explaining the FILESEL.MAK project so that you'll know how to build such file-selecting frames for your user, and you'll know how to access the selected information
after the user finishes selecting a file.
Figure 17.3. This book's FILESEL.MAK project with the file-selecting frame on the screen.
The next section, "The File Controls," explains how
to use the file controls. Each of the file controls produces a different kind of file-access list box. Figure 17.3 includes callouts to the three list boxes produced by the file controls that
you'll learn about in the next section.
Analysis: The FILESEL.MAK application demonstrates how you might implement a file-selection frame. The application does nothing more than display the file-selection frame when the user clicks the Display File
command
button, and when the user selects a file, the application erases the frame and displays a new command button labeled You Selected... so that the user can see the file, path, and drive of the file selected from the frame earlier.
This particular
application allows the user to select files but not enter new filenames. In other words, this application forms the basis for a frame that enables the user to open files that already exist. If you wanted to give the user the ability to
enter a name not in
the list, you would have to modify the application to look in the File Name text box for a new and valid file name.
The FILESEL.MAK program uses the frame's Visible property, setting the property to True for displaying the frame and to False when
the frame must not appear on the screen.
Concept: Visual Basic
includes three file controls that you manage from a file-selection frame.
Figure 17.4 shows the Toolbox window showing the location of the three file controls. As you know, selecting a file consists not only of choosing a filename but also
choosing a directory and a drive. When you place these file controls on a frame,
remember that you must draw each control by dragging with the mouse (explained more fully in Lesson 6's second unit, "Checks, Options, and Control Arrays") so that
each control stays with the frame when code moves, hides, or displays the frame.
Figure 17.4. The file controls on the Toolbox window.
Using the file-related controls requires that you tie them together
through code. In other words, when the user changes the drive name, both the path name and the file list must change also to show the paths and files on the newly selected drive.
Table 17.1 lists the properties that appear in the Properties
window for the drive list box control. The properties are similar to the ones that you've learned for other controls. The drive list box control contains a list of disk drives that are
available on the system running the program. As with any list box
control, the user selects from the drive list box at runtime, so you can't set a default drive in the list until runtime.
Property | Description |
BackColor | Specifies the drive list box's background color, chosen as a hexadecimal color code or from the color palette. |
DragIcon | Contains the icon that appears when the user drags the drive list box around on the form. (You'll only rarely allow the user to move a drive list box, so the Drag... property settings aren't usually relevant.) |
DragMode | Holds either 1 for manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the drive list box but that you, through code, can initiate the dragging if needed. |
Enabled | Determines whether the drive list box can respond to events. If set to True (the default), the drive list box can respond to events. Otherwise, Visual Basic halts event processing for that particular control. |
FontBold | Holds True (the default) if the drive are to display in boldfaced characters; False otherwise. |
FontItalic | Holds True (the default) if the drive names are to display in italicized characters; False otherwise. |
FontName | Contains the name of the drive list box drive name's styles. Typically, you'll use the name of a Windows TrueType font. |
FontSize | Holds the size, in points, of the font used for the drive names. |
FontStrikethru | Holds True (the default) if the drive names are to display in strikethru letters (characters with dashes through them); False otherwise. |
FontUnderline | Holds True (the default) if the drive names are to display in underlined letters; False otherwise. |
ForeColor | Specifies the color of the letters in the drive names. |
Height | Contains the height, in twips, of the drive list box. |
HelpContextID | If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text. |
Index | If the drive list box is part of a control array, the Index property provides the numeric subscript for each particular drive list box. (See Lesson 6.) |
Left | Contains the number of twips from the left edge of the Form window to the left edge of the drive list box. |
MousePointer | Holds the shape that the mouse cursor changes to if the user moves the mouse cursor over the drive list box. The possible values are from 0 to 12 and represent a range of different shapes that the mouse cursor can take. (See Lesson 12.) |
Name | Contains the name of the control. By default, Visual Basic generates the names Drive1, Drive2, and so on as you add subsequent drive list boxes to the form. |
TabIndex | Determines whether the focus tab order begins at 0 and increments every time you add a new control. You can change the focus order by changing the controls' TabIndex to other values. No two controls on the same form can have the same TabIndex value. |
TabStop | If True, determines whether the user can press Tab to move the focus to this drive list box. If False, the drive list box can't receive the focus. |
Tag | Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the drive list box. |
Top | Holds the number of twips from the top edge of a drive list box to the top of the form. |
Visible | Holds True or False, indicating whether the user can see (and, therefore, use) the drive list box. |
Width | Holds the number of twips wide that the drive list box consumes. |
At runtime, you can set the drive list box's Drive property. The Drive property will contain the name of the drive as well as the volume label on the drive. If you want only the drive letter, use the Left$() function to pick
off the drive letter
from the left of the drive's Drive property.
Table 17.2 lists the properties that appear in the Properties window for the file list box control. Many of the properties are similar to the ones that you've learned for other
controls, but a few are important file-specific properties that you'll
control using Visual Basic code.
Property | Description |
Archive | If True (the default), specifies that the file list contains files whose internal archive (backed up) properties are set. If False, files whose archive bits are not set are not displayed. |
BackColor | Contains the file list box's background color, chosen as a hexadecimal color code or from the color palette. |
DragIcon | Holds the icon that appears when the user drags the file list box around on the form. (You only rarely allow the user to move a file list box, so the Drag... property settings aren't usually relevant.) |
DragMode | Either contains 1 to indicate manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the file list box but that you, through code, can initiate the dragging if needed. |
Enabled | If set to True (the default), determines whether the file list box can respond to events. Otherwise, Visual Basic halts event processing for that particular control. |
FontBold | Holds True (the default) if the file names are to display in boldfaced characters; False otherwise. |
FontItalic | Holds True (the default) if the file names are to display in italicized characters; False otherwise. |
FontName | Contains the name of the file list box filename's styles. Typically, you'll use the name of a Windows TrueType font. |
FontSize | Specifies the size, in points, of the font used for the filenames. |
FontStrikethru | Holds True (the default) if the filenames are to display in strikethru letters (characters with dashes through them); False otherwise. |
FontUnderline | Holds True (the default) if the filenames are to display in underlined letters; False otherwise. |
ForeColor | Specifies the color of the letters in the filenames. |
Height | Holds the height, in twips, of the file list box. |
HelpContextID | If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text. |
Hidden | If True, specifies that the file list contains files whose internal hidden properties are set. If False (the default), files whose hidden properties are not set are displayed. |
Index | If the file list box is part of a control array, the Index property provides the numeric subscript for each particular file list box. (See Lesson 6.) |
Left | Holds the number of twips from the left edge of the Form window to the left edge of the file list box. |
MousePointer | Contains the shape that the mouse cursor changes to if the user moves the mouse cursor over the file list box. The possible values are from 0 to 12 and represent a range of different shapes that the mouse cursor can take. (See Lesson 12.) |
MultiSelect | If MultiSelect contains 0-None (the default), the user can select only one filename. If 1-Simple, the user can select more than one filename by clicking with the mouse or by pressing the spacebar over filenames in the list. If 2-Extended, the user can select multiple filenames using the Shift+click and Shift+arrow to extend the selection from a previously selected filename to the current filename, and Ctrl+click either selects or deselects a filename from the list. |
Name | Holds the name of the control. By default, Visual Basic generates the names File1, File2, and so on as you add subsequent file list boxes to the form. |
Normal | If the Normal contains True (the default), the file list contains files whose internal normal properties are set. If False, files whose archive bits are not set to normal are not displayed. |
Pattern | Contains a wildcard pattern of files. * in the pattern indicates all files, and ? means all characters. *.txt means all files whose extensions are txt, and sales??.dat means all files whose first five letters are sales, the sixth and seventh letters of the filenames can be anything, and the extension must be dat. |
ReadOnly | Contains True (the default) if the file list is to contain files whose internal read-only properties are set. Therefore, the file list displays on those files that can't be changed. If contains False, files whose read-only properties are not set are not displayed. |
System | Holds True if the file list contains files whose internal system properties are set. If False (the default), files whose system properties are not set are displayed. |
TabIndex | Specifies the focus tab order begins at 0 and increments every time that you add a new control. You can change the focus order by changing the controls' TabIndex to other values. No two controls on the same form can have the same TabIndex value. |
TabStop | Contains True if the user can press Tab to move the focus to this file list box. If False, the file list box can't receive the focus. |
Tag | Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the file list box. |
Top | Holds the number of twips from the top edge of a file list box to the top of the form. |
Visible | Contains either True or False, indicating whether the user can see (and, therefore, use) the file list box. |
Width | Holds the number of twips wide that the file list box consumes. |
Table 17.3 lists the properties that appear in the Properties window for the directory list box control. Many of the properties are similar to the ones that you've learned for other controls. As with the file list control, you can set and select
values from directory list boxes at runtime.
Property | Description |
BackColor | Specifies the directory list box's background color, chosen as a hexadecimal color code or from the color palette. |
DragIcon | Specifies the icon that appears when the user drags the directory list box around on the form. (You'll only rarely let the user move a directory list box, so the Drag... property settings aren't usually relevant.) |
DragMode | Contains either 1 for manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the directory list box but you, through code, can initiate the dragging if needed. |
Enabled | Specifies that the directory list box can respond to events if set to True (the default). If set to False, Visual Basic halts event processing for that particular control. |
FontBold | Holds True (the default) if the directory names are to display in boldfaced characters; False otherwise. |
FontItalic | Holds True (the default) if the directory names are to display in italicized characters; False otherwise. |
FontName | Contains the name of the directory list box directory name's styles. Typically, you'll use the name of a Windows TrueType font. |
FontSize | Specifies the size, in points, of the font used for the directory names. |
FontStrikethru | Holds True (the default) if the directory names are to display in strikethru letters (characters with dashes through them); False otherwise. |
FontUnderline | Holds True (the default) if the directory names are to display in underlined letters; False otherwise. |
ForeColor | Specifies the color of the letters in the directory names. |
Height | Holds the height, in twips, of the directory list box. |
HelpContextID | If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text. |
Index | If the directory list box is part of a control array, the Index property provides the numeric subscript for each particular directory list box. (See Lesson 6.) |
Left | Holds the number of twips from the left edge of the Form window to the left edge of the directory list box. |
MousePointer | Specifies the shape that the mouse cursor changes to if the user moves the mouse cursor over the directory list box. The possible values are from 0 to 12 and represent a range of different shapes that the mouse cursor can take. (See Lesson 12.) |
Name | Contains the name of the control. By default, Visual Basic generates the names Dir1, Dir2, and so on as you add subsequent directory list boxes to the form. |
TabIndex | Specifies that the focus tab order begins at 0 and increments every time you add a new control. You can change the focus order by changing the controls' TabIndex to other values. No two controls on the same form can have the same TabIndex value. |
TabStop | If True, specifies that the user can press Tab to move the focus to this directory list box. If False, the directory list box can't receive the focus. |
Tag | Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the directory list box. |
Top | Holds the number of twips from the top edge of a directory list box to the top of the form. |
Visible | Holds True or False, indicating whether the user can see (and, therefore, use) the directory list box. |
Width | Holds the number of twips wide that the directory list box consumes. |
Note: At runtime, you can set the directory list box's Path property. The Path property contains the name of the disk drive as well as the full path.
Most of the file-related controls are common and control the placement, size, and look of controls. However, you'll have to constantly update the values of key properties for
these file-related controls as the user selects different values from the
controls.
For example, when the user selects a different drive name, you'll have to update both the directory control's list of directories as well as the file list
control's list of filenames. When the user selects a different directory, you'll have to update
the file list control's list of filenames but not change the drive list control. Keeping these file controls in sync is the only unusual requirement for using
these controls. The next section explains how the FILESEL.MAK application uses code to tie
together its file controls.
Note: The events available for the file controls are extremely similar to those that you've seen. The most commonly coded event procedures are Click and DblClick. In addition, the drive and directory list boxes support the Change event procedure that executes when the user changes the drive or directory in some way.
Review: Although many properties for the file-related controls are similar to those that you know from other controls, there are a few key
properties that determine the selection from which the user can select
filenames. The following properties are especially critical:
The next section shows how the FILESEL.MAK application keeps these controls in synchronization while the user selects among drives, directories, and files.
Concept: When one file selection
control changes, such as the drive list box control, that change usually affects the other file controls. You must learn how to code file controls to keep them all pointing to
the same drive, directory, and file.
When the user clicks the Display
Files command button, the Click event procedure in Listing 17.1 executes to set up the default drive and path.
Listing 17.1. The initial code for the file-selection frame's display.
1: Sub cmdDisp_Click () 2: ' Set default values 3: drvDrive.Drive = "c:" 4: dirList.Path = "c:\" ' Root directory 5: 6: ' Display the file-selection frame 7: lblDirs.Caption = dirList.Path 8: txtFiles.Text = "*.txt" 9: fraFile.Visible = True 10: ' Select the first file 11: ' in the list of files 12: filFile.ListIndex = 0 13: End Sub
By setting the Drive and Path properties of the drive list box and the directory list box in
lines 3 and 4, the command button sets up a default listing for the C: drive and the root directory on the C: drive. Line 7 initializes the label that shows the
selected directory name above the directory box (directly beneath the Directories: label).
Line 8 initializes the File Name text box with a default file pattern. A common filename extension for text files is txt, so the initial pattern is set to *.txt.
Line 9 does the work of displaying the frame to the user. During the FILESEL.MAK's
development, the frame's Visible property was set to False, so the user doesn't see the frame or its controls until the user presses the command button. Finally, line 12
selects the first item in the file list box so that a filename is always selected in
the list upon the initial display of the frame. If no files exist for the given drive, directory, and pattern, line 12 has no effect on the program.
Tip: Manage the drive change first.
When writing a file-selection frame, always code the drive change event procedure before anything else. When the user changes the drive, you must ensure that the directory list changes
as well. Listing 17.2 contains the Change event procedure for the
drive.
Listing 17.2. Code that executes when the user selects a different drive.
1: Sub drvDrive_Change () 2: dirList.Path = drvDrive.Drive 3: End Sub
The drvDrive_Change() event procedure may not seem to do much. Actually, the purpose of the procedure is to trigger another event. As soon as the user changes the drive, Listing 17.2
works to update the path of the directory to the new drive's
directory (the current default directory that's selected on the newly chosen drive). Therefore, when the user selects a new drive, Listing 17.2 acts to update the directory list, which
in turn triggers the directory's Change event procedure
shown in Listing 17.3.
Listing 17.3. When the directory changes, so must the file list.
1: Sub dirList_Change () 2: ' The selection of the directory 3: ' list box changed 4: filFile.Path = dirList.Path 5: ' Make sure that one file is selected 6: If (filFile.ListCount > 0) Then 7: ' Select the first file 8: ' in the list of files 9: filFile.ListIndex = 0 10: End If 11: 12: lblDirs.Caption = dirList.Path 13: End Sub
Line 4 updates the file list box to reflect the path of the new directory chosen. Lines 5 through 10 ensures that a file is selected, even if the user
didn't select one. Line 9 assigns the selection to the first file in the file list. Line 12 updates
the pathname caption that appears beneath the Directories label showing the current directory. The chain reaction is now tied together so that a change in
the drive changes both the directory and the file list. If the user changes only the directory and
not the drive, the change in the directory (due to the dirList_Change() event procedure) changes also.
There is one Change event that's not tied
to this chain-reaction of drive-directory-file change, but one that's important to maintain in all your applications. If the user wants to see a different set of files in the current directory, the user can
enter a new file pattern in the File Name text
box. As soon as the user enters a new pattern, the Change event procedure shown in Listing 17.4 executes.
Listing 17.4. The user just changed the filename pattern.
1: Sub txtFiles_Change () 2: filFile.Pattern = txtFiles.Text 3: End Sub
Any change in the File Name text box produces an immediate corresponding change in the list of files shown. Therefore, as soon as the user changes the default pattern from
*.txt to *.exe, for example, the filename list box updates to show only those
files whose extensions are .exe.
The user is allowed to close the file-selection dialog using any of the following three methods:
Listing 17.5 contains the Click event procedure for the OK command button. When the user closes the file-selection frame, several things
must take place. The highlighted filename needs to be saved. Line 3 saves the highlighted filename by placing that
filename in the File Name text box. Even though the frame will be hidden (as well as all controls on the frame, including the text box) by
line 5, the text box will be available to the program, and any routine in the application can access txtFiles.Text to
see what filename the user selected. In addition, the drive and directory controls will still hold their selected values, even though the
user won't be able to see these controls.
Listing 17.5. The OK closing routine.
1: Sub cmdFileOK_Click () 2: ' Save the file selected by the user 3: txtFiles.Text = filFile.List(filFile.ListIndex) 4: ' Hide the file selection frame 5: fraFile.Visible = False 6: ' Tell surrounding code that Cancel was not pressed 7: DidCancel = False ' User didn't press Cancel 8: ' Show the "You Selected" command button 9: cmdSel.Visible = True 10: End Sub
There is a module variable named DidCancel, defined in the form module's (general) procedure, that holds either True or False, depending on whether the user clicks Cancel. If Listing 17.5 executes, the
user selected OK, not Cancel, so line 7 sets the
DidCancel variable to False in case any application using this frame needs to know whether Cancel was pressed. Line 9 closes out the event procedure by hiding the file selection frame and all its controls.
If the user double-clicks a filename, the one-line DblClick event procedure shown in Listing 17.6 executes. The DblClick event procedure does nothing more than call the OK command button's Click event procedure because the same result occurs: the
file
selected by the user becomes the requested file and the frame disappears from the user's view.
Listing 17.6. The user selected a file by double-clicking the filename.
1: Sub filFile_DblClick () 2: ' Double-clicking a selected filename 3: ' does the same thing as selecting a 4: ' file and pressing the OK button. 5: ' Therefore, call the OK button's Click event. 6: Call cmdFileOK_Click 7: End Sub
If the user clicks the Cancel command button, nothing should change in the program except that the frame disappears and the DidCancel variable should be set to True. Therefore, if you add this frame and its event procedures to your own
applications, the
DidCancel variable tells you whether the user pressed Cancel. Listing 17.7 shows the Cancel command button's Click procedure.
Listing 17.7. The user clicked Cancel to close the file-selection
frame.
1: Sub cmdCancel_Click () 2: ' Used pressed the Cancel button 3: ' Hide the frame and inform the program 4: DidCancel = True 5: fraFile.Visible = False 6: End Sub
Review: The code behind the file selection frame describes how the file controls work in conjunction with each other. A user's change to one of the file controls can affect the other file controls. Use code to
control
those additional changes so that a drive or directory change updates a corresponding file list box.
Please remember that if you adopt this file-selection frame for your own application, the disk drive appears at the start of the pathname.
Therefore, you don't have to read the drive list box as long as you use the full path supplied by the path list
box's Path property.
Change the FILESEL.MAK to enable the user to select from files that already exist or enter a new file name. You'll have to add to the frame's closing
code so that the program checks the text box entry against the selected file list.