1. Home
  2. Computing & Technology
  3. Delphi Programming

Using TFilterComboBox with TShellTreeView
Filter Files and Folders being Displayed by the Shell Tree View

By Zarko Gajic, About.com

Filter TShellTreeView

The TShellTreeView Delphi component displays a hierarchical tree view of the system's shell folders. By default it will display only folders, and when paired with the TShellListView it enables you to build a Windows Explorer clone.

The ObjectTypes property of the ShellTreeView allows for Non-Folder types to be also displayed by the Shell Tree View.

When an item is being added to the shell tree, the AddFolder event is raised. The CanAdd parameter can be set to false to true to either display the folder/file or not.

To filter the files in the shell tree view, by file extension, the TShellTreeView can be used with the TFilterComboBox ("Win31" palette).

For a test application, drop a TFilterComboBox and a TShellTreeView on a Delphi form. Use ObjectInspector to specify some filters for the FilterComboBox, then use the AddFolder event to filter the ShellTreeView.

The follwing methods are event handlers for FilterComboBox.OnChange, ShellTreeView.AddFolder and Form.OnCreate.

//refresh the Shell Tree View when Filter (Mask) changes
procedure TFilterTreeForm.FilterComboBox1Change(Sender: TObject) ;
begin
   ShellTreeView1.Refresh(ShellTreeView1.Items.GetFirstNode) ;
end;

//Show or Hide the Folder/File depending on the selected mask
procedure TFilterTreeForm.ShellTreeView1AddFolder(
   Sender: TObject;
   AFolder: TShellFolder;
   var CanAdd: Boolean) ;
var
   maskExt : string;
   fileExt : string;
begin
   maskExt := ExtractFileExt(FilterComboBox1.Mask) ;

   if maskExt = '*.*' then
   begin
     CanAdd := true;
     Exit;
   end;

   fileExt := ExtractFileExt(AFolder.DisplayName) ;

   CanAdd := AFolder.IsFolder OR (CompareText(maskExt,fileExt) = 0) ;
end;

//let ShellTreeView display the files, also
procedure TFilterTreeForm.FormCreate(Sender: TObject) ;
begin
   ShellTreeView1.ObjectTypes := [otNonFolders] + ShellTreeView1.ObjectTypes;
end;
Note: The TShellTreeView is not installed with Delphi, you need to manually install the Shell package.

Delphi tips navigator:
» Reset the TWebBrowser Delphi control to an Empty (Blank) Page
« How to Create the "!DocType" and "?XML" Elements using TXmlDocument

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. Using TFilterComboBox with TShellTreeView to Filter Files and Folders being Displayed by the Shell Tree View

©2009 About.com, a part of The New York Times Company.

All rights reserved.