1. Computing

Drag Multiple Files From Delphi's TShellListView

Get The List Of Selected Files When TShellListView.MultiSelect is True

From , former About.com Guide

TShellListView.MultiSelect

TShellListView.MultiSelect

TShellListView.MultiSelect

The TShellListView control (a part of the Shell Controls package) can be used to display files from the file system. Paired with TShellTreeView shell, the TShellListView can be used to mimic Windows Explorer user interface.

The full name (path) of the selected file is stored in the PathName property of the SelectedFolder property. That is, if a file is selected in a shell list view, to get its name you can read: ShellListView1.SelectedFolder.PathName.

Note that SelectedFolder is of the TShellFolder type, and its IsFolder property will actually tell you if the selected item is a folder or a file.

MultiSelect in TShellListView

When MultiSelect property is set (to true) for the shell list view, to get the list of selected files, you need to iterate through items and check if an item is selected.

Here's an example of drag-dropping selected files from the TShellListView to a TMemo control. Drop a TMemo control on a form, name it "memoSelectedFiles". Drop a TShellListView.

Handle Memo's OnDragDrop and OnDragOver events as:

//handles Memo's OnDragDrop
procedure TSimpleFilesForm.memoSelectedFilesDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  files: TStringList;
  slv : TShellListView;
  cnt: Integer;
begin
  slv := TShellListView(Source);

  files := TStringList.Create;
  try
    if NOT slv.MultiSelect then
      files.Add(slv.SelectedFolder.PathName)
    else //multiselect
    begin
      for cnt := 0 to -1 + slv.Items.Count do
      begin
      //selected and NOT folder
        if slv.Items[cnt].Selected AND (NOT slv.Folders[cnt].IsFolder) then
          files.Add(slv.Folders[cnt].PathName);
        end;
    end;

    //display selected files in memo
    memoSelectedFiles.Lines.Assign(files);
  finally
    files.Free;
  end;
end;

//handles Memo's OnDragOver
procedure TSimpleFilesForm.memoSelectedFilesDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TShellListView;
end;
Test: select a few files in the shell list view and drag them over the memo, than drop. In the memo, you'll see a list of file names being selected / dropped.

Read Understanding Drag and Drop Operations for a better understanding of OnDragDrop and OnDragOver events and their handling.

Delphi tips navigator:
» Deactivating TShellTreeView
« Selecting Delphi's TListView Item on Item Check / UnCheck

©2013 About.com. All rights reserved.