DELPHI - How to use opendialog1 for choosing a folder?

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; if SelectedFolder[ Length( SelectedFolder ) ] <> '\' then
SelectedFolder := SelectedFolder + '\';

uses FileCtrl;
const
SELDIRHELP = ;
procedure TForm1.Button1Click(Sender: TObject);
var
Dir: string;
begin
Dir := 'C:\Windows';
if FileCtrl.SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then
Label1.Caption := Dir;
end;

Selecting a directory with TOpenDialog

ust found the code below that seems to work fine in XP and Vista, Win7.

It provides a UI for a user to select a directory.

It uses TOpenDialog, but sends it a few messages to clean up the appearance for the purposes of selecting a directory.

After suffering from the limited capabilities provided by Windows itself,

it's a pleasure to be able to give my users a familiar UI where they can browse and select a folder comfortably.

I'd been looking for something like this for a long time so thought I'd post it here so others can benefit from it.

Here's what it looks like in Win 7:

function GimmeDir(var Dir: string): boolean;
var
OpenDialog: TOpenDialog;
OpenDir: TOpenDir;
begin
//The standard dialog...
OpenDialog:= TOpenDialog.Create(nil);
//Objetc that holds the OnShow code to hide controls
OpenDir:= TOpenDir.create;
try
//Conect both components...
OpenDir.Dialog:= OpenDialog;
OpenDialog.OnShow:= OpenDir.HideControls;
//Configure it so only folders are shown (and file without extension!)...
OpenDialog.FileName:= '*.';
OpenDialog.Filter:= '*.';
OpenDialog.Title:= 'Chose a folder';
//No need to check file existis!
OpenDialog.Options:= OpenDialog.Options + [ofNoValidate];
//Initial folder...
OpenDialog.InitialDir:= Dir;
//Ask user...
if OpenDialog.Execute then begin
Dir:= ExtractFilePath(OpenDialog.FileName);
result:= true;
end else begin
result:= false;
end;
finally
//Clean up...
OpenDir.Free;
OpenDialog.Free;
end;
end;

最新文章

  1. Lesson 3 Please send me a card
  2. C# socket通信
  3. JDBC数据类型
  4. 转 Delphi中使用FastMM4结合View CPU避免内存泄漏
  5. 编程之美----NIM游戏
  6. 关于form验证的处理片断
  7. loj 1021(状压dp+记忆化搜索)
  8. hdu 1094 A+B for Input-Output Practice (VI)
  9. Eclipse的安装以及与Tomcat的集成
  10. 面向报文(UDP)和面向字节流(TCP)的区别
  11. [转]ubuntu搭建LAMP环境
  12. Excel列A、B、C、D----与列序号的转换
  13. .NET-ORM框架EF-Code First代码优先
  14. 【收藏】UICrawler
  15. POI SXSSFWorkbook 读取模板 存在公式解决
  16. 雷林鹏分享:C# 字符串(String)
  17. [转]关于Linux安装mysql默认配置文件位置
  18. Scala--数组相关操作
  19. SharePoint Online 创建文档库
  20. 团队作业之现场UML设计

热门文章

  1. Android的layout_weight和weightSum
  2. linux下搭建我的世界spongeforge 服务器 (海绵端)
  3. 记一次HashMap面试
  4. 一个无锁消息队列引发的血案(三)——地:q3.h 与 RingBuffer
  5. cas:覆盖安装
  6. ubuntu 12.04网络配置之设置静态iP
  7. shell中后台进程管理: ps -aux 详解
  8. 再谈CentOS 7程序自启动
  9. redux,react-redux、redux-thunk、redux-logger、redux-promise实例
  10. Ubuntu 下 vi 输入方向键会变成 ABCD 的解决方法