Freetutes.com - Free Visual basic Tutorial website on the web

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Sponsored Links

The File Controls in Visual Basic

 
Three of the controls on the ToolBox let you access the computer's file system. They are DriveListBox, DirListBox and FileListBox controls (see below figure) , which are the basic blocks for building dialog boxes that display the host computer's file system. Using these controls, user can traverse the host computer's file system, locate any folder or files on any hard disk, even on network drives. The files are controls are independent of one another, and each can exist on it's own, but they are rarely used separately. The files controls are described next.

Following figure shows three files controls are used in the design of Forms that let users explore the entire structure of their hard disks.

* DriveListBox : Displays the names of the drives within and connected to the PC. The basic property of this control is the drive property, which set the drive to be initially selected in the control or returns the user's selection.

* DirListBox : Displays the folders of current Drive. The basic property of this control is the Path property, which is the name of the folder whose sub folders are displayed in the control.

* FileListBox : Displays the files of the current folder. The basic property of this control is also called Path, and it's the path name of the folder whose files are displayed.

The three File controls are not tied to one another. If you place all three of them on a Form, you will see the names of all the folders under the current folder, and so on. Each time you select a folder in the DirlistBox by double clicking its name, its sub folders are displayed. Similarly , the FileListBox control will display the names of all files in the current folder. Selecting a drive in the DriveListBox control, however this doesn't affect the contents of the DirListBox.

To connect to the File controls, you must assign the appropriate values to the properties. To compel the DirListBox to display the folders of the selected drive in the DriveListBox, you must make sure that each time the user selects another drive, the Path property of the DirListBox control matches the Drive property of the DriveListBox.

The following is the minimum code you must place in the DriveListBox control's change event :

Private Sub Drive1_Change( )
Dir1.Path = Drive1.Drive
End Sub

Similarly, every time in the current selection in DirListBox control changes, you must set the FileListBox control's path property to point the new path of the DirListBox control :

Private Sub Dir1_Change( )
File1.Path = Dir1.Path
End Sub

This is all it takes to connect the three file controls. Following example shows the use of File controls.

* Open a New project and save the Form as FileControls.frm and save the Project as FileControls.vbp.

* Design the application as shown above.

The following code is typed in the Change events of the DriveListBox and DirListBox

Save the project and run it by pressing F5. You can see that the folders and files are shown according to your selection.

(Download the source code)

Previous Page | Table of Contents | Next Page

 

 

 

   
Home | Link to Us | Partner Links | About Us | Contact Us
Copyright © 2007-2008 Freetutes.com | All Rights Reserved