Jump to content
Incus J

Choose a Folder dialog

Recommended Posts

Does Delphi 10.4 FMX have a built in routine or component to display a Choose a Folder dialog?  I can see dialog components to open and save a file, but not to select a folder.  If there isn't anything built in, how might I achieve this?

 

I'm looking for something that will work on both Windows and macOS.  I have found some code online, and the Windows side of it works, but it looks fairly elaborate, and the macOS code crashes the app with Exception class 6.  So perhaps there is an easier approach using existing routines.  I'm running 10.4 Update 1 at present.

Edited by Incus J

Share this post


Link to post
4 hours ago, Incus J said:

Does Delphi 10.4 FMX have a built in routine or component to display a Choose a Folder dialog?

No.

4 hours ago, Incus J said:

If there isn't anything built in, how might I achieve this?

IFDEF your code to call the relevant OS APIs directly (SHBrowseForFolder()/IFileOpenDialog on Windows, etc).  Or else just make your own dialog/Form that iterates the filesystem (ie, via the System.IOUtils.TDirectory class) and display the contents as needed.

 

  • Thanks 1

Share this post


Link to post

Perhaps SelectDirectory() from FMX.Dialogs could be beneficial.

 

It is implemented for Windows and macOS platforms.

It is not implemented for Android nor iOS.

 

var
  LDirectory : String;

  if SelectDirectory('Test' {Title}, '' {Root}, LDirectory {out}) then
  begin
    // selected..
    ShowMessage(LDirectory);
  end;

 

Edited by f.m
  • Like 2
  • Thanks 1

Share this post


Link to post

Thank you Remy, Wil, f.m,

 

The code I found online is the same fmxexpress example Wil mentions above, so I've been playing around with it.  If I comment out the following block, then it works without crashing the app:

if ADir <> '' then begin
  LInitialDir := TNSURL.Create;
  LInitialDir.initFileURLWithPath(NSSTR(ADir));
  LOpenDir.setDirectoryURL(LInitialDir);
end;

The crash was occurring on LInitialDir.initFileURLWithPath(NSSTR(ADir)); and when I stepped through the code ADir appeared to be '' empty, so I'm not sure how this block was executing at all.  I guess without this block I'm losing the capability for specifying an initial starting folder.

 

I also get a warning that NSSTR is deprecated, but the suggested replacement StrToNSStr is not recognised.  Is there an easy way to query which system unit a function might be in?

Share this post


Link to post
FMX.Dialogs

function SelectDirectory(const Caption: string; const Root: string;
  var Directory: string): Boolean;

Thank you f.m - that's exactly what I'm looking for!

Share this post


Link to post
15 minutes ago, Incus J said:

I also get a warning that NSSTR is deprecated, but the suggested replacement StrToNSStr is not recognised.  Is there an easy way to query which system unit a function might be in?

Hi,

 

I have that same code and don't see a crash, not sure what else is different.

re. the StrtoNSStr, you have to use the correct module for it to be recognized. If I'm not mistaken it is in:

 Macapi.Helpers,

 

re. suggestion from f.m. thanks for the tip, I will check it out.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×