Link to home
Start Free TrialLog in
Avatar of aztec
aztec

asked on

Something like OpenDialog, but for a directory

Hi...
 When I click a button, I want my app to display an OpenDialog box similar to selecting a file, but for selecting a *directory* instead (only show directories, not files). How can I do this?

Thanks
   Shawn
Avatar of Stuart_Johnson
Stuart_Johnson

Hi Shawn,

Try this:

function GetPath: String;
var
 pl: PItemIDList;
 folder : array[0..MAX_PATH] of Char;
 bi  : TBrowseInfo;
begin
  Result := '';
  FillChar(bi,SizeOf(bi),#0);
  StrPCopy(Folder, PathEdit.Text);
  bi.pszDisplayName := @folder[0];
  bi.lpszTitle := 'Please Select a Folder';
  bi.ulFlags := BIF_RETURNONLYFSDIRS;
  bi.hwndOwner := ExportToParadoxForm.handle;
  pl := SHBrowseForFolder(bi);
  if Assigned(pl) then
    if SHGetPathFromIDList(pl, folder) then
      Result := Folder;
  CoTaskMemFree(pl);
end;

Hope this helps,

Cheers,

Stu.
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kotan,

How?  What do I declare in Uses?  SelectDirectory doesn't even exist in the help file.  Can you give some more info?

Stu.
My appologies, it does :)

That's a nice little function.  Looks like a wrapper for the code I posted above (which I got from Madshi I think from memory).

Speaking of which, I just noticed an error:

function GetPath: String;
var
pl: PItemIDList;
folder : array[0..MAX_PATH] of Char;
bi  : TBrowseInfo;
begin
 Result := '';
 FillChar(bi,SizeOf(bi),#0);
 StrPCopy(Folder, PathEdit.Text);
 bi.pszDisplayName := @folder[0];
 bi.lpszTitle := 'Please Select a Folder';
 bi.ulFlags := BIF_RETURNONLYFSDIRS;
 bi.hwndOwner := Form1.handle;
 pl := SHBrowseForFolder(bi);
 if Assigned(pl) then
   if SHGetPathFromIDList(pl, folder) then
     Result := Folder;
 CoTaskMemFree(pl);
end;

The Owner Handle was pointing to one of my forms.

Stu
Avatar of kretzschmar
SelectDirectory already exists,
(or was it selectdir)
i guess its in the sysutils-unit

meikl ;-)
SelectDirectory is in FileCtrl-unit
yep, kotan
the code stu posted is the nicest (IMHO) cause you can customize it by changing the TBrowseInfo structure.
add shlobj and activex to the uses for it to work.

also delete this line its not nessesary :
StrPCopy(Folder, PathEdit.Text);
listening
Aztec,

Why don't you just make your own.
I've attached a unit file and a form file with the code.

Note:
1) This was done very quickly, you may want to enhance it.
2) When setting the directory of the dialog form make sure you send it the full path name, the path has to end with a file name (ex c:\temp\test.txt) or a directory ending with a "\" (ex. c:\temp\ )

Hope this helps

Gerhard

---Start unit (PAS) file----
unit Unit3;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, FileCtrl;

type
  TOKBottomDlg = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    procedure DriveComboBox1Change(Sender: TObject);
  private
    FDirectory: string;
    procedure SetDirectory(const Value: string);
    function GetDirectory: string;
    { Private declarations }
  public
    { Public declarations }
    property Directory: string read GetDirectory write SetDirectory;
  end;

var
  OKBottomDlg: TOKBottomDlg;

implementation

{$R *.DFM}

procedure TOKBottomDlg.DriveComboBox1Change(Sender: TObject);
begin
  DirectoryListBox1.Drive := DriveComboBox1.Drive;
end;

function TOKBottomDlg.GetDirectory: string;
begin
  Result := DirectoryListBox1.Directory;
end;

procedure TOKBottomDlg.SetDirectory(const Value: string);
begin
  FDirectory := Value;
  DriveComboBox1.Drive := ExtractFileDrive(FDirectory)[1];
  DirectoryListBox1.Directory := ExtractFilePath(FDirectory);
end;

end.
-----END UNIT FILE----

---START FORM (DFM) FILE----
object OKBottomDlg: TOKBottomDlg
  Left = 314
  Top = 244
  BorderStyle = bsDialog
  Caption = 'Dialog'
  ClientHeight = 214
  ClientWidth = 313
  Color = clBtnFace
  ParentFont = True
  OldCreateOrder = True
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Bevel1: TBevel
    Left = 8
    Top = 8
    Width = 297
    Height = 161
    Shape = bsFrame
  end
  object OKBtn: TButton
    Left = 79
    Top = 180
    Width = 75
    Height = 25
    Caption = 'OK'
    Default = True
    ModalResult = 1
    TabOrder = 0
  end
  object CancelBtn: TButton
    Left = 159
    Top = 180
    Width = 75
    Height = 25
    Cancel = True
    Caption = 'Cancel'
    ModalResult = 2
    TabOrder = 1
  end
  object DriveComboBox1: TDriveComboBox
    Left = 16
    Top = 16
    Width = 281
    Height = 19
    Anchors = [akLeft, akTop, akRight]
    TabOrder = 2
    OnChange = DriveComboBox1Change
  end
  object DirectoryListBox1: TDirectoryListBox
    Left = 16
    Top = 40
    Width = 281
    Height = 121
    ItemHeight = 16
    TabOrder = 3
  end
end
---END OF FORM FILE----
I guess the only reason I'd suggest using what I posted over a custom form is that it keeps the "windows" feel.  In other words, it looks like every other application's directory browser.

Thanks for pointing out the bit of code I left in there, Barry.  Oops :)

Stu
What about adding a callback to display the situation to the user?
Avatar of aztec

ASKER

Kotan - that works pretty nicely, thanks. Anyway to suppress the file listing somehow?

Maybe there's a 3rd party custom component out there that does this?

Thanks
  Shawn

aztec,

Did you try what I had posted?

Stu
Avatar of aztec

ASKER

No I didn't Stuart - Kotan's seemed like the quickie solution I was looking for. Yours will suppress the file listing?

Shawn
Avatar of aztec

ASKER

Can't find any info in Help under TBrowseInfo Stuart. Does it exist in Delphi 3 Pro?

Shawn
Aztec

It depends on how you code it.  The way I presented the code to you now displays a dialog box with directories only.  You can suppress or display the files if you wish by changing the TBrowseInfo flags.

Sometimes the quickest way isn't always the best way.

Stu
Aztec,

Seem like there are 2 version of SelectDirectory(). One will show files and the other one is not.

Please refer below links
http://members.truepath.com/delphi/tips/tip105_selectdirectoryi1.htm
http://members.truepath.com/delphi/tips/tip106_selectdirectoryii1.htm
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them updated as the collaboration effort continues, to maintain your open and locked questions.  If you are a  KnowledgePro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer which does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item for help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20245156.html
https://www.experts-exchange.com/questions/Q.20259219.html
https://www.experts-exchange.com/questions/Q.20263069.html
https://www.experts-exchange.com/questions/Q.20270808.html
https://www.experts-exchange.com/questions/Q.20269981.html
https://www.experts-exchange.com/questions/Q.20277592.html
https://www.experts-exchange.com/questions/Q.20279091.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20263454.html
https://www.experts-exchange.com/questions/Q.20269773.html
https://www.experts-exchange.com/questions/Q.20279093.html

**** PLEASE DO NOT AWARD THE POINTS TO ME. *****
 
------------>  EXPERTS:  Please leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> https://www.experts-exchange.com/commspt/Q.20277028.html
 

Moderators will finalize this question if in @7 days you have not responded.  They will either move this to the PAQ (Previously Asked Questions) at zero points, delete it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange