DELPHI - How to use opendialog1 to select a folder?

Possible duplicate:
Delphi: selecting a directory using the TOpenDialog function

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

wanted - open folder dialog in Delphi

PS: I am using Delphi 2010

+4
source share
3 answers

You can also use the TBrowseForFolder action TBrowseForFolder ( 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 the WinApi function - SHBrowseForFolder directly (the second SelectDirectory overload uses it instead of the first overload, which creates its own delphi window with all the 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; 
+6
source

In Vista and above, you can show a more modern look of dialogue 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 is as follows:

enter image description here

+13
source

You are looking for SelectDirectory in the FileCtrl module. 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 are using, and on the specific look and functionality you are looking for; I (as a rule, believe that the second version works fine for modern versions of Delphi and Windows, and users are satisfied with the "normally expected appearance and functionality".

+9
source

Source: https://habr.com/ru/post/1399307/


All Articles