1. Computing & Technology

Discuss in my forum

Using TFilterComboBox with TShellTreeView

Filter Files and Folders being Displayed by the Shell Tree View

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.