...realize a MultiSelect in ShellListView?
|
Autor:
Owen Barnes |
[ Print tip
] | | |
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ShellListView does not have a files property (common in dialogs eg TOpendialog),
therefore we have to cycle through each item to find out whether the
item is selected or not.
Use this function to obtain a list of selected files in a ShellListView:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
function SelectedFiles(AShellView: TShellListView): TStringList;
begin
Result := TStringList.Create;
for i := 0 to shelllistview1.Items.Count - 1 do
// is the item selected?
if shelllistview1.Items[i].Selected = True then
// Folders can also refer to files, which is why we check isFolder
// before adding the filepath to the result
if shelllistview1.folders[i].IsFolder = False then
// add filepath and filename to result
Result.Add(shelllistview1.Folders[i].PathName);
end;
// ---------- Usage: -------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
FileList: TStringList;
begin
try
FileList := TStringList.Create; //create stringlist to contain filenames
FileList := SelectedFiles(ShellListView1); //populate tstringlist
if FileList.Count = 0 then Exit; //exit if no files selected
for i := 0 to FileList.Count - 1 do
ShowMessage(FileList[i]); //cycle through each filename and do something
finally
FreeAndNil(FileList); //free tstringlist when finished
end;
end;
|