Search code examples
delphi-7

Is it possible to select a folder/directory using TOpenDialog?


I've seen this question: Selecting a directory with TOpenDialog

Which technically is NOT answered (OP asked SPECIFICALLY about TOpenDialog - not TFileOpenDialog) but is applicable to later versions of Delphi.

I'm using Delphi 7 so TFileOpenDialog is not available.

So is it possible to use TOpenDialog to select a folder?

I know about SelectDirectory and have seen the other postings on that and I also know about BrowseFolder.

I am ONLY interested in answers that pertain to TOpenDialog.

I tried setting the Filename property to '*.' as someone suggested somewhere but that doesn't work.


Solution

  • I've seen this question: Selecting a directory with TOpenDialog

    Which technically is NOT answered

    It is, well in one of the comments anyway:

    "TFileOpenDialog != TOpenDialog ... TOpenDialog doesn't have such an option"

    That is the answer.

    I'm using Delphi 7 so TFileOpenDialog is not available.

    No, it is not. However, the underlying IFileDialog and IFileOpenDialog interfaces that it uses internally are standard Win32 COM interfaces (on Vista+ only), and as such they can be used in Delphi 7 just fine, as long as you have their declarations in your code.

    So is it possible to use TOpenDialog to select a folder?

    The short answer is NO.

    In Delphi 7, TOpenDialog is just a wrapper for the Win32 API GetOpenFileName() function, which can only select and return files, not folders. You must use SelectDirectory() (which is just a wrapper for the Win32 API SHBrowseForFolder() function if you use the newer overload), or IFileDialog/IFileOpenDialog with the FOS_PICKFOLDERS option enabled.

    In modern versions of Delphi, TOpenDialog does delegate to IFileDialog/IFileOpenDialog on Vista+ whenever possible (theming enabled, not using old VCL features that not expose by the newer dialog, etc), but it does not enable the FOS_PICKFOLDERS option.