Use Delphi's File and Directory Controls to Create a Windows Explorer

Build custom Explorer-like forms with file system components

Windows Explorer is what you use in the Windows operating system to browse for files and folders. You can create a similar structure with Delphi so that the same content is populated within your program's user interface.

Common dialog boxes are used in Delphi to open and save a file in an application. If you want to use customized file managers and directory browsing dialogs, you 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: 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 the Dialog Form

Start a new Delphi application and select the Win 3.1 tab of the Component palette. Then do the following:

  • Place one TFileListBox, TDirectoryListBox, TDriveComboBox, and TFilterComboBox component on a form, keeping all of their default names
  • Add one TEdit (named "FileNameEdit") and one TLabel (call it "DirLabel").
  • Include a few labels with captions, like "File Name," "Directory," "List Files of Type," and "Drives."

To show the currently selected path as a string in a DirLabel components caption, assign the Label's name to the DirectoryListBox's DirLabel property.

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

More Lines of Code

When you have all the file system components on the form, you just 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. Make it look like this:

 procedure TForm1.DriveComboBox1Change(Sender: TObject) ;
DirectoryListBox1.Drive := DriveComboBox1.Drive;

This code changes the display in the DirectoryListBox by activating its OnChange event Handler:

 procedure TForm1.DirectoryListBox1Change(Sender: TObject) ;
FileListBox1.Directory := DirectoryListBox1.Directory;

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

 procedure TForm1.FileListBox1DblClick(Sender: TObject) ;
Showmessage('Selected: '+ FileListBox1.FileName) ;

Remember that the Windows convention is to have a double-click choose the file, not a single click. 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 the 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's 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 runtime can be also be done at design time. You 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 its caption property in the DirectoryListBox's OnChange event to the DirectoryListBox.Directory property.