Solved

Something like OpenDialog, but for a directory

Posted on 2002-03-19
19
404 Views
Last Modified: 2010-04-05
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
Comment
Question by:aztec
  • 6
  • 3
  • 3
  • +6
19 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
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
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
Comment Utility
Use this function

function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload;
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
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
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
SelectDirectory already exists,
(or was it selectdir)
i guess its in the sysutils-unit

meikl ;-)
0
 
LVL 6

Expert Comment

by:kotan
Comment Utility
SelectDirectory is in FileCtrl-unit
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
yep, kotan
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
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
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
listening
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:Gerhard100198
Comment Utility
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
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
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
 
LVL 3

Expert Comment

by:SteveWaite
Comment Utility
What about adding a callback to display the situation to the user?
0
 

Author Comment

by:aztec
Comment Utility
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
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
aztec,

Did you try what I had posted?

Stu
0
 

Author Comment

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

Shawn
0
 

Author Comment

by:aztec
Comment Utility
Can't find any info in Help under TBrowseInfo Stuart. Does it exist in Delphi 3 Pro?

Shawn
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
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
 
LVL 6

Expert Comment

by:kotan
Comment Utility
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
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Create a Restore Point In Windows 10 3 102
IExtractImage Delphi 14 162
Thread safe  opinion 7 110
Delphi Form ownership 4 50
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now