![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
This last characteristic of the DirListBox is the one well use. It enables us to obtain the names of the current folders subfolders. Apply a recursive algorithm as follows:
This general type of algorithm is called recursion. The same procedures are applied over and over to the data until no more data remains to process. In this case, the data consists of folder names. As youll see in the demonstration program, the code required to implement the file search is actually quite simple. As is often the case in computer programming, having the proper algorithm is an important step in designing code. Creating The FormOne important Visual Basic technique illustrated by this project is the use of hidden controls. Just because a control is hidden (Visible property = False) does not mean it is nonfunctional. Of course, the user cannot interact with it, but it can be used by code. In this project, you will use a hidden DirListBox control to implement the file-search algorithm. Start a new Standard EXE project. Add a DriveListBox and two DirListBox controls, setting the Visible property of one of the DirListBoxes to False. Add a Check Box, a List Box, and a control array of three Command Buttons. Finally, add one Text Box and three labels to identify the Text Box, the visible DirListBox, and the DriveList Box. The form should look more or less like Figure 14.4. The DirListBox superimposed on the ListBox is the one that will be hidden. Set control and form properties as shown in Listing 14.5. Please note the new format in Listing 14.5. This is the format used by Visual Basic within a form file to record the forms controls and their properties. If you open a FRM file in a text editor, youll see the controls and properties listed at the top of the file and the Basic code listed at the end. Starting with this project, I will sometimes be presenting controls and properties in this more efficient format rather than detailing them individually in the text. While the original FRM file lists all of the properties for each control, I will include only those that you need to change from their default values. By looking through a control/property listing, such as Listing 14.5, you can determine the various property settings you need to make. Listing 14.5 Objects and properties in FINDFILE.FRM. Begin VB.Form Form1 Appearance = 0 3D BorderStyle = 1 Fixed Single Caption = File Finding Utility Begin VB.ListBox lstFiles BeginProperty Font name = Courier New size = 9.75 EndProperty End Begin VB.CheckBox chkFileInfo Caption = Include file information in list End Begin VB.DirListBox dirHidden Visible = False End Begin VB.DirListBox dirStart End Begin VB.DriveListBox Drive1 Appearance = 1 3D End Begin VB.CommandButton Command1 Caption = &Quit Index = 2 End Begin VB.CommandButton Command1 Appearance = 0 Flat Caption = Sto&p Index = 1 End Begin VB.CommandButton Command1 Appearance = 0 Flat Caption = &Start Index = 0 End Begin VB.TextBox txtPattern Text = Text1 End Begin VB.Label Label3 Alignment = 1 Right Justify Caption = File template: End Begin VB.Label Label2 Alignment = 1 Right Justify Caption = Starting directory: End Begin VB.Label Label1 Alignment = 1 Right Justify Caption = Drive: End End Writing The CodeThe program requires two global variables. The first is a type Boolean flag that signals whether the user has canceled the search; the other is a type Integer that keeps track of the depth of the searchin other words, how many folders deep execution is. These variables should be declared in the general declarations section of code, shown in Listing 14.6.
Listing 14.6 General declarations in FINDFILE.FRM. Option Explicit Global flag to abort search. Dim Halt As Boolean Dim Depth As Integer The program actions are triggered by the Click event procedure for the Command Button control array. Its quite simple, as you can see in Listing 14.7. To start the search, it calls the procedure StartSearch. To terminate an ongoing search, it sets the flag variable Halt to True. To exit the program, it executes the End statement. Listing 14.7 The Command Button Click event procedure. Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 Start button Call StartSearch Case 1 Stop button Halt = True Case 2 Exit button End End Select End Sub The StartSearch procedure is where the action begins. This is a general procedure that you add to the module with the Insert Procedure command. Its code is shown in Listing 14.8. First, code in this procedure clears the List Box where the search results will be displayed and sets the Halt flag to False. It then disables all of the controls that should not be available during a search. Next, turn your attention to the hidden DirListBox control. It is this controls Change event procedure that triggers the actual search action. You need to set this controls Path property to the folder that the user specified as the start folder for the search, obtained from the Path property of the visible DirListBox. This property change will normally trigger the controls Change event. But what if the hidden DirListBox controls Path property is already pointing to the desired start directory? Because no Change event will be generated, well have to generate one ourselves (remember that event procedures can be called in code just like nonevent procedures). Here is the code: If dirHidden.Path = dirStart.Path Then dirHidden_Change Else dirHidden.Path = dirStart.Path End If Note that the program displays the hourglass mouse pointer while the search is in progress. Once that Change event is triggered for the hidden DirListBox control, execution recurses within event procedures until the search is completed. Then it returns to the same location in the StartSearch procedure. The final lines of code in this procedure reenable the forms controls that were disabled when the search began, returning the normal mouse pointer.
|
![]() |
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. Read EarthWeb's privacy statement. |