Wednesday, December 12, 2012

Drive or file control in visual basic


The File Controls: Three of the controls on the Toolbox let you access the computer’s file system. They are the DriveListBox, DirListBox, and FileListBox controls. Using these controls, the user can traverse the host computer’s file system, locate any folders or files on any hard disk, even on network drives.

Three File 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 sets the drive to be initially selected in the control or returns the user’s selection.
  DirListBox Displays the folders of the current drive. The basic property of this control is the Path property, which is the name of the folder whose subfolders 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 aren’t tied to one another. To connect the File controls, you must assign the appropriate values to their basic properties. To force 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 the current selection in the DirListBox control changes, you must set the FileListBox control’s Path property to point to the new path of the DirListBox control:
Private Sub Dir1_Change()
   File1.Path = Dir1.Path
End Sub
you’ll want to limit the files displayed in the FileListBox. To do this, use the control’s Pattern property, which lets you specify which files will be displayed with a file-matching string such as “*.TXT” or “1997*.XLS”.
File1.Pattern = Combo1.Text