You are here:About>Computing & Technology>Delphi Programming> Coding Delphi Applications> Delphi Tips and Tricks> 2005 Delphi Tips> Using TFilterComboBox with TShellTreeView to Filter Files and Folders being Displayed by the Shell Tree View
About.comDelphi Programming
Filter TShellTreeView
Newsletters & RSSEmail to a friendSubmit to Digg

Using TFilterComboBox with TShellTreeView

From Zarko Gajic,
Your Guide to Delphi Programming.
FREE Newsletter. Sign Up Now!

Filter Files and Folders being Displayed by the Shell Tree View

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

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.