Create your own Windows Explorer using Delphi's File and Directory controls

Creating custom Explorer-like forms with file system components

Delphi File Controls in Action

It's all about files, directories, reading and writing

In one of the previous articles, we were using the advantage of the Common Dialog Boxes for opening and saving files in Delphi applications.

If we want to use customized file managers and directory browsing dialogs, we'll have to deal with file system Delphi components. The Win 3.1 VCL palette group includes several components that allow you to build your own custom File Open or File Save dialog box.

The components are TFileListBox, TDirectoryListBox, TDriveComboBox, and TFilterComboBox.

Navigating Files

The file system components allow us to select a drive, see the hierarchical directory structure of a disk, and see the names of the files in a given directory.

All of the file system components are designed to work together. For example, your code checks what the user has done to, say, a DriveComboBox and then passes this information on to a DirectoryListBox. The changes in DirectoryListBox are then passed to a FileListBox in which the user can select the file(s) needed.

Designing Dialog Form ...

Start a new Delphi application and select the Win 3.1 tab of the Component palette.
  • Place one TFileListBox, TDirectoryListBox, TDriveComboBox, and TFilterComboBox component on a form. Keep all the component names, that Delphi provides as the defaults.
  • We'll also need: one TEdit (name: FileNameEdit), one TLabel (name: DirLabel)
  • Plus a few labels with captions: 'File Name', 'Directory', 'List Files of Type' and 'Drives'.
To show the currently selected path as a string in a DirLabel components caption, we assign the Label's name to the DirectoryListBox's DirLabel property.

If we want to display the selected filename in an EditBox (FileNameEdit) we have to assign the Edit object's Name (FileNameEdit) to the FileListBox's FileEdit property.

... Plus a Few Lines of Code

Now, when we have all the file system components on a form, we merely have to set the DirectoryListBox.Drive property and the FileListBox.Directory property in order for the components to communicate and show what the user wants to see.

For example, when the user selects a new drive, Delphi activates the DriveComboBox OnChange event handler. Let it look like:

 procedure TForm1.DriveComboBox1Change(Sender: TObject) ;
 begin
   DirectoryListBox1.Drive := DriveComboBox1.Drive;
 end; 
This code changes the display in the DirectoryListBox by activating its OnChange event Handler:
 procedure TForm1.DirectoryListBox1Change(Sender: TObject) ;
 begin
   FileListBox1.Directory := DirectoryListBox1.Directory;
 end; 
This code updates the FileListBox.

In order to see what file the user has selected we use the OnDblClick event of the FileListBox:

 procedure TForm1.FileListBox1DblClick(Sender: TObject) ;
 begin
   Showmessage('Selected: '+ FileListBox1.FileName) ;
 end; 
Remember that the Windows convention is to have a double-click, not a single click, choose the file. This is important when you work with a FileListBox, because using an arrow key to move through a FileListBox would call any OnClick handler that you have written.

Filtering Display

Use a FilterComboBox to control the type of files that are displayed in a FileListBox. After setting the FilterComboBox's FileList property to the name of a FileListBox, set the Filter property to the file types that you want to display. Here is a sample filter:
 FilterComboBox1.Filter := 'All files (*.*)|*.* | Project files (*.dpr)|*.dpr | Pascal units (*.pas)|*.pas'; 

Hints and Tips

Setting the DirectoryListBox.Drive property and the FileListBox.Directory property (in the previously written OnChange event handlers) at run time, can be also done at design time. We can accomplish this kind of connection at design time by setting the following properties (from the Object Inspector):

DriveComboBox1.DirList := DirectoryListBox1
DirectoryListBox1.FileList := FileListBox1

Users can select multiple files in a FileListBox if its MultiSelect property is True. The following code shows how to create a list of multiple selections in a FileListBox and show it in a SimpleListBox (some "ordinary" ListBox control).

 var k: integer;
 ...
 with FileListBox1 do
   if SelCount > 0 then
    for k:=0 to Items.Count-1 do
     if Selected[k] then
       SimpleListBox.Items.Add(Items[k]) ; 

To display full path names that are not shortened with an ellipsis, do not assign a Label object name to the DirLabel property of a DirectoryListBox. Instead, insert a Label into a form and set it's caption property in the DirectoryListBox's OnChange event to the DirectoryListBox.Directory property.

That's all .. now you can build your own Explorer! But wait, here are a few more Delphi controls you can use, that look even more like Windows Explorer: Shell Controls.