[Overview][Types][Classes][Procedures and functions][Index] Reference for unit 'ShellCtrls' (#lcl)

TCustomShellTreeView.OnSortCompare

Event handler signalled to compare file items in a custom sort routine.

Declaration

Source position: shellctrls.pas line 140

public property TCustomShellTreeView.OnSortCompare : TFileItemCompareEvent
  read FOnSortCompare
  write SetOnSortCompare;

Description

OnSortCompare is a TFileItemCompareEvent property with the event handler signalled to implement a custom file sort for the tree view control. OnSortCompare is used to order the directories or files displayed in the tree view control when its FileSortType property is set to fstCustom.

Changing the routine assigned to the property causes the Items in the control to be reloaded and ordered using the new file sort compare routine.

An application can implement and assign a routine using the signature perform custom file comparison routines using various attributes. The following is an item compare function, as implemented by forum member d7_2_laz, used to order items with leading Underscore characters:

function TForm1.SortCompareUnderscore(Item1, Item2: TFileItem): integer;
begin
  // Make sure that folders are moved to the top
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
    if (pos('_', Item1.FileInfo.Name) = 1) or 
      (pos('_', Item2.FileInfo.Name) = 1) then
      Result := AnsiCompareText(Item1.FileInfo.Name, Item2.FileInfo.Name)
    else
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
end;

Sort File Items by Date

function TForm1.SortCompareByDate(Item1, Item2: TFileItem): integer;
begin
  // Folders first ...
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
  begin
    // then file date ...
    Result := CompareValue(Item1.FileInfo.TimeStamp, Item2.FileInfo.TimeStamp);
    if Result = 0 then
      // then file name
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
  end;
end;

Sort File Items by Size

function TForm1.SortCompareBySize(Item1, Item2: TFileItem): integer;
begin
  // Folders first
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
  begin
    // then file size ...
    Result := Item1.FileInfo.Size - Item2.FileInfo.Size;
    if Result = 0 then
      // then file name
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
  end;
end;

Version info

Added in LCL version 3.0.

See also

TCustomShellTreeView.FileSortType

  

Indicates how the items should be sorted in the tree.

TCustomShellTreeView.Items

  

The container with the TTreeNode instances for the control.

TCustomShellTreeView.Path

  

Path to the directory displayed in the shell control.

TCustomShellTreeView.PopulateTreeNodeWithFiles

  

Adds tree nodes for file system objects found starting at the specified node / path.

TCustomShellTreeView.PopulateWithBaseFiles

  

Fills the tree view when the Root directory is empty.

TCustomShellTreeView.Root

  

Indicates the directory to start showing the list of items.