[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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
0
aztec
Asked:
aztec
  • 6
  • 3
  • 3
  • +6
1 Solution
 
Stuart_JohnsonCommented:
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.
0
 
kotanCommented:
Use this function

function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload;
0
 
Stuart_JohnsonCommented:
Kotan,

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

Stu.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
Stuart_JohnsonCommented:
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
0
 
kretzschmarCommented:
SelectDirectory already exists,
(or was it selectdir)
i guess its in the sysutils-unit

meikl ;-)
0
 
kotanCommented:
SelectDirectory is in FileCtrl-unit
0
 
kretzschmarCommented:
yep, kotan
0
 
intheCommented:
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);
0
 
ginsonicCommented:
listening
0
 
Gerhard100198Commented:
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----
0
 
Stuart_JohnsonCommented:
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
0
 
SteveWaiteCommented:
What about adding a callback to display the situation to the user?
0
 
aztecAuthor Commented:
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

0
 
Stuart_JohnsonCommented:
aztec,

Did you try what I had posted?

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

Shawn
0
 
aztecAuthor Commented:
Can't find any info in Help under TBrowseInfo Stuart. Does it exist in Delphi 3 Pro?

Shawn
0
 
Stuart_JohnsonCommented:
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
0
 
kotanCommented:
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
0
 
MoondancerCommented:
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://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.
http://www.experts-exchange.com/questions/Q.20245156.html
http://www.experts-exchange.com/questions/Q.20259219.html
http://www.experts-exchange.com/questions/Q.20263069.html
http://www.experts-exchange.com/questions/Q.20270808.html
http://www.experts-exchange.com/questions/Q.20269981.html
http://www.experts-exchange.com/questions/Q.20277592.html
http://www.experts-exchange.com/questions/Q.20279091.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20263454.html
http://www.experts-exchange.com/questions/Q.20269773.html
http://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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://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
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 6
  • 3
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now