19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

292 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

The same program with a style 0 combo box produces the output shown.<br />

DRIVE, DIRECTORY, AND FILE LIST BOX CONTROLS<br />

Boxes similar to those inside the Open Project dialog box of Figure 10-1 are available to any<br />

<strong>Visual</strong> <strong>Basic</strong> program via icons from the Toolbox. <strong>Visual</strong> <strong>Basic</strong> does much of the work of providing<br />

the appropriate lists for the three boxes. Windows determines the contents of the drive<br />

box. The programmer determines the contents of directory list boxes <strong>and</strong> file list boxes with<br />

Path properties.<br />

Most of the properties, methods, <strong>and</strong> events of list boxes are also valid for the three filerelated<br />

list boxes. For instance, in the file list box File1, File1.ListCount is the number of<br />

files <strong>and</strong> File1.List(n) is the name of the nth file, where counting begins with 0. The selected<br />

items for the three controls are identified by the Drive, Path, <strong>and</strong> FileName properties,<br />

respectively. For instance, in the drive list box Drive1, the selected drive is given by the string<br />

Drive1.Drive.<br />

Suppose a form contains a drive list box named Drive1, a directory list box named Dir1,<br />

<strong>and</strong> a file list box named File1. (These names are the default names supplied by <strong>Visual</strong><br />

<strong>Basic</strong>.) When the user selects a new drive from the Drive1 list box, the directories in Dir1<br />

should reflect this change. The proper event procedure to effect the change is<br />

Private Sub Drive1_Change()<br />

Dir1.Path = Drive1.Drive<br />

End Sub<br />

This event is triggered by clicking on the drive name or using the arrow keys to highlight the<br />

drive name <strong>and</strong> then pressing Enter. When the user selects a new directory in Dir1, the files<br />

in File1 can be changed with the event procedure<br />

Private Sub Dir1_Change()<br />

File1.Path = Dir1.Path<br />

End Sub<br />

This event procedure is triggered by double-clicking on a directory name. If the preceding<br />

two event procedures are in place, a change of the drive will trigger a change of the directory,<br />

which in turn will trigger a change in the list of files. The st<strong>and</strong>ard prefixes for the names<br />

of the drive, directory, <strong>and</strong> file list box controls are drv, dir, <strong>and</strong> fil, respectively.<br />

EXAMPLE 4<br />

The following program can be used to display the full name of any file on any drive.<br />

Object Property Setting<br />

frmFiles<br />

drvList<br />

dirList<br />

filList<br />

Caption Select a File<br />

cmdDisplay<br />

picFileSpec<br />

Caption Display Complete Name of File

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!