Mason Wheeler
Mason Wheeler

Reputation: 84650

How to get the current selection in a TCustomFileDialog?

I've got a TFileOpenDialog with an OnSelectionChange event hooked up, trying to take certain actions based on the selection. It fires every time the selected item in the shell view box is changed, but the FileName property seems to always contain the name of the most recently selected file.

If you select a folder the FileName property does not get updated. I understand that this is because a folder is not a file.

So clearly FileName is the wrong thing to read. Obviously there's something in there that knows what's currently actually selected. How do I get at that?

Upvotes: 0

Views: 327

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597951

procedure TMyForm.DialogBoxSelectionChange(Sender: TObject);
var
  HRes: HRESULT;
  Attr: DWORD;
begin
  if DialogBox.ShellItem <> nil then
  begin
    HRes := DialogBox.ShellItem.GetAttributes(SFGAO_FILESYSTEM or SFGAO_FOLDER, Attr);
    if Succeeded(HRes) then
    begin
      if (Attr and SFGAO_FOLDER) = 0 then
      begin
        // must be a file, display info as needed
        Exit;
      end;
    end;
  end;
  // clear info as needed...
end;

Upvotes: 3

Mason Wheeler
Mason Wheeler

Reputation: 84650

I managed to figure it out.

var
  item: IShellItem;
  filename: PChar;
begin
  DialogBox.Dialog.GetCurrentSelection(item);
  if item = nil then
  begin
    //nothing is selected; handle appropriately
  end
  else begin
    item.GetDisplayName(SIGDN_FILESYSPATH, filename);
    //handle appropriately
  end;
end;

Upvotes: 0

Related Questions