DELPHI - How to use opendialog1 for choosing a folder? [duplicate]
Asked Answered
B

3

6

Possible Duplicate:
Delphi: Selecting a directory with TOpenDialog

I need to open a specific folder on my project. When I use opendialog1, I can only open a file. How about opening a folder ?

wanted - open folder dialog in Delphi

PS : I use Delphi 2010

Boxcar answered 2/3, 2012 at 3:58 Comment(3)
Ken's answer (the only one so far) is great, but this seems like a dup of: #7423189Pentyl
In the fact, you can use the TOpenDialog descendant - TSaveDialog (yeah, thats quite quick and dirty)Garlandgarlanda
Voted to close, but I'll be missing teran's answer there.Biomedicine
E
6

You also can use TBrowseForFolder action class (stdActns.pas):

var
  dir: string;
begin
  with TBrowseForFolder.Create(nil) do try
    RootDir  := 'C:\';
    if Execute then
      dir := Folder;
  finally
    Free;
  end;
end;

or use WinApi function - SHBrowseForFolder directly (second SelectDirectory overload uses it, instead of first overload, which creates own delphi-window with all controls at runtime):

var
  dir : PChar;
  bfi : TBrowseInfo;
  pidl : PItemIDList;
begin
  ZeroMemory(@bfi, sizeof(bfi));
  pidl := SHBrowseForFolder(bfi);
  if pidl <> nil then try
    GetMem(dir, MAX_PATH + 1);
    try
      if SHGetPathFromIDList(pidl, dir) then begin
        // use dir
      end;
    finally
      FreeMem(dir);
    end;
  finally
    CoTaskMemFree(pidl);
  end;
end;
Edieedification answered 2/3, 2012 at 6:43 Comment(1)
lines 13,14 could be replaced with CoTaskMemFreeGarlandgarlanda
C
19

On Vista and up you can show a more modern looking dialog using TFileOpenDialog.

var
  OpenDialog: TFileOpenDialog;
  SelectedFolder: string;
.....
OpenDialog := TFileOpenDialog.Create(MainForm);
try
  OpenDialog.Options := OpenDialog.Options + [fdoPickFolders];
  if not OpenDialog.Execute then
    Abort;
  SelectedFolder := OpenDialog.FileName;
finally
  OpenDialog.Free;
end;

which looks like this:

enter image description here

Cayes answered 2/3, 2012 at 7:35 Comment(0)
C
12

You're looking for SelectDirectory in the FileCtrl unit. It has two overloaded versions:

function SelectDirectory(var Directory: string; 
   Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
function SelectDirectory(const Caption: string; const Root: WideString; 
var Directory: string; Options: TSelectDirExtOpts; Parent: TWinControl): Boolean;

The one you want to use depends on the version of Delphi you're using, and the specific appearance and functionality you're looking for; I( usually find the second version works perfectly for modern versions of Delphi and Windows, and users seem happy with the "normally expected appearance and functionality".

Carrillo answered 2/3, 2012 at 4:10 Comment(4)
+1 Ken, btw the unit name is FileCtrl.Papacy
Both functions were moved out of the FileCtrl unit a long time ago.Choragus
Rodrigo, thanks for the correction. Fixed. @Remy, the docs for XE2 say you're wrong. If they were moved "a long time ago" the docs should mention that fact.Carrillo
Nevermind. Other FileCtrl functions were indeed moved to SysUtils a long time ago and their FileCtrl references were deprecated. I thought SelectDirectory() was amongst them, but I just checked and that is not the case afterall.Choragus
E
6

You also can use TBrowseForFolder action class (stdActns.pas):

var
  dir: string;
begin
  with TBrowseForFolder.Create(nil) do try
    RootDir  := 'C:\';
    if Execute then
      dir := Folder;
  finally
    Free;
  end;
end;

or use WinApi function - SHBrowseForFolder directly (second SelectDirectory overload uses it, instead of first overload, which creates own delphi-window with all controls at runtime):

var
  dir : PChar;
  bfi : TBrowseInfo;
  pidl : PItemIDList;
begin
  ZeroMemory(@bfi, sizeof(bfi));
  pidl := SHBrowseForFolder(bfi);
  if pidl <> nil then try
    GetMem(dir, MAX_PATH + 1);
    try
      if SHGetPathFromIDList(pidl, dir) then begin
        // use dir
      end;
    finally
      FreeMem(dir);
    end;
  finally
    CoTaskMemFree(pidl);
  end;
end;
Edieedification answered 2/3, 2012 at 6:43 Comment(1)
lines 13,14 could be replaced with CoTaskMemFreeGarlandgarlanda

© 2022 - 2024 — McMap. All rights reserved.