Visual Basic Concepts
The directory list box displays the directory structure of the current drive on the user's system, beginning with the top-level directory. Initially, the name of the current directory appears highlighted and indented from directories above it in the hierarchy, back to the root. Subdirectories are indented beneath the current directory in the directory list box. As a user moves up or down the list, each of the items is highlighted in turn.
Each directory in the box has an integer identifier associated with it that allows you to identify individual directories. This capability is not provided by the common dialog control. The directory specified by the Path property (Dir1.Path
) always has the ListIndex value of – 1. The directory immediately above it has the ListIndex value of – 2, the one above that of – 3, and so on up to the root. The first subdirectory of Dir1.Path
has the ListIndex 0. If there are multiple directories at the first subdirectory level, the next has the ListIndex value of 1, then 2, and so on, as shown in Figure 7.18.
Figure 7.18 A directory structure displayed in the directory list box
Use the Path property of the directory list box to set or return the current directory in the box (ListIndex = – 1). For example, if you assign "c:\payroll" to Drive1.Path
in Figure 7.18, the \Payroll directory becomes selected as the current working directory.
Similarly, you can assign the Drive property of the drive list box to the Path property of the directory list box:
Dir1.Path = Drive1.Drive
When this assignment is executed, the directory list box displays all the available directories and subdirectories on that drive. By default, the directory list box also displays all directories above, and any subdirectories immediately below, the current directory of a drive assigned to the Dir1.Path
property. The directory list box doesn't set the current directory at the operating system level; it merely highlights the directory and gives it the ListIndex value of – 1.
To set the current working directory, use the ChDir statement. For example, the following statement changes the current directory to the one displayed in the directory list box:
ChDir Dir1.Path
In an application that uses file controls, you can set the current directory to the directory where the application's executable (.exe) file is located with the Application object:
ChDrive App.Path ' Set the drive.
ChDir App.Path ' Set the directory.
Note The Path property is available only at run time, not at design time.
For More Information See "App Object" for more information on the Application object.
When a user clicks an item in a directory list box, that item is highlighted. When an item is double-clicked, it is assigned to the Path property, its ListIndex property gets the value –1, and the directory list box is redrawn to show its immediate subdirectories.
The ListCount property returns the number of directories below the currently expanded directory, not the total number of items in the directory list box. Because the ListIndex value of the currently expanded directory is always –1, you can write code to determine how far down from the root the currently expanded directory is in the hierarchy. For example:
' Initialize for currently expanded directory.
GoHigher = 0
' Dir1.List(x) returns empty string if the directory
' doesn't exist.
Do Until Dir1.List(GoHigher) = ""
GoHigher = GoHigher - 1
Loop
' Convert to positive number, if desired.
LevelsAbove = Abs(GoHigher)