File-System Controls Scenario: A File Seeker Application

Because users often want to find a file or group of files available to an application quickly, many applications provide capabilities for investigating the file system. The Winseek.vbp sample application helps the user browse drives and directories, and displays any category of files.

Figure 7.19   File-system controls in the WinSeek application

The following table summarizes the controls in Seek.frm from the WinSeek application.

Control Property Setting
Drive list box Name drvList
Directory list box Name dirList
File list box Name
Pattern
filList
*.*
First command button Name
Caption
Default
cmdSearch
&Search
True
Second command button Name
Caption
cmdExit
E&xit
List box Name lstFoundFiles

Note   The file-system controls do not have caption properties, although you can label them and give them access keys. For more information on using labels this way, see "Using the Label Control" later in this chapter.

Writing Code for the WinSeek Application

In the drive list box, a Change event is triggered by a single mouse click on an item. A Change event also occurs when the user selects an item and then changes the focus on the form. In the directory list box, a DblClick event is necessary to generate a Change event.

When users want to change directories without using a mouse, they typically use the arrow keys to select the desired directory and then press the ENTER key.

Because ENTER is commonly associated with the default command button control, WinSeek must recognize when the user simply wants to change directories rather than conduct a search for files.

The WinSeek application resolves this ambiguity by determining if the path of the dirList box differs from the currently highlighted directory. This situation can occur when the user single-clicks an item in the directory list box or navigates the directory list box using the arrow keys. The following code determines whether the dirList.Path is different from the path of the highlighted directory. If the paths are different, the dirList.Path is updated. If the paths are the same, the search is performed.

Private Sub cmdSearch_Click()
   .
   .
   .
' If the dirList.Path is different from the currently 
' selected directory, update it; otherwise perform the 
' search.
If dirList.Path <> dirList.List(dirList.ListIndex) Then
   dirList.Path = dirList.List(dirList.ListIndex)
   Exit Sub
End If
' Continue with search.
   .
   .
   .
End Sub

The WinSeek application uses the following procedures to handle significant events:

The Drive List Box's Change Event

When the user clicks an item in the drive list box, its Change event is generated. The drvList_Change event procedure is invoked, and the following code is run:

Private Sub drvList_Change ()
   On Error GoTo DriveHandler
   ' If new drive was selected, the Dir1 box 
   ' updates its display.
   dirList.Path = drvList.Drive
   Exit Sub   
' If there is an error, reset drvList.Drive with the
' drive from dirList.Path.
DriveHandler:
   drvList.Drive = dirList.Path
   Exit Sub   
End Sub

Notice that the Change event in a drive list box occurs when a new drive is selected, either with a single mouse click or when the user moves the selection (for example, with an arrow key). The error handler is triggered by actions such as attempting to access a floppy disk drive while the drive door is open or selecting a network drive that has been inadvertently disconnected. Because the error prevents the original assignment, dirList.Path still contains the previous valid drive. Reassigning dirList.Path to drvList.Drive corrects this error.

For More Information   See "Debugging Your Code and Handling Errors" for more information.

The Directory List Box's Change Event

If the user double-clicks an item in the directory list box, or if the Path property of dirList is changed in code (as in the drvList_Change procedure), the dirList_Change event is initiated. The following code responds to that event:

Private Sub dirList_Change ()
   ' Update file list box to synchronize with the 
   ' directory list box.
   filList.Path = dirList.Path
End Sub

This event procedure assigns the Path property of the dirList box to the Path property of the filList box. This causes a PathChange event in the filList list box, which is redrawn; you don't need to add code to the filList_PathChange procedure, because in this application, the event chain ends in the filList list box.

The Command Button's Click Event

This event procedure determines whether the highlighted item in the dirList list box is the same as the dirList.Path. If the items are different, then dirList.Path is updated. If the items are the same, then the search is performed.

Private Sub cmdSearch_Click ()
   .
   .
   .
   ' If the dirList.Path is different from the 
   ' currently selected directory, update it; 
   ' otherwise perform the search.
   If dirList.Path <> dirList.List _
   (dirList.ListIndex) Then
      dirList.Path = dirList.List(dirList.ListIndex)
      Exit Sub
   End If
   ' Continue with search.
   .
   .
   .
End Sub

Note   You can enhance the WinSeek application with additional features. For example, you might want to use a file control's attribute properties. You could use check boxes to allow the user to set different combinations of file attributes so that the file list box displays files that are Hidden, System, and so on. This would restrict a search to conforming files.