Title: Selecting files
Question: In which ways can you let your user choose a file or directory?
Answer:
Opening files and directories
Selecting files and directories.
Selecting files and directories.
The opening of files is one of the most common tasks in any application. Whether you develop your own word processor, an email application, or something simple like your own dvd inventory, there is usually a moment
when you want to open a file or to select a directory.
You have probably done so in several applications already, and perhaps think you know everything. If you are fully satisfied, skip this article.
On the other hand, if there have been some minor dissatisfactions with what you
have realized, this article may be of interest.
There are lots of ways to let your user select a file or directory. This article shows 5 ways to
implement this, and mentions some pros and cons. This articles does not delve
into the various ways Delphi provides to actualy open a file.
1. Win3.1
The old fashioned way, dating back to windows 3.x and Delphi 1, is by slapping a TDriveComboBox, TDirectoryListBox and TFileListBox on a form. Set the DirList property of the TDriveComboBox to DirectoryListBox1, the FileList property of the TDirectoryListBox to FileListBox1, and you run!. You can still do this in Delphi 5, and probably also in Delphi 6 and 7.
The advantages of this method are that it develops quick, and I never encountered any problems. Also, you dont need to do any coding. I still see it used in many, many modern applications.
There are also disadvantages. For a start, it definitely looks old fashioned. Second, your user misses many of the features of windows
explorer, such as 'My Computer', 'My network places' and such.
2. Rx
The Rx components are a set of freely available components. You can download them from http://sourceforge.net/projects/rxlib/
After installing them, you will find a TFileNameEdit and a TDirectoryEdit component on the Rx Controls page.
These components allow your user to select a filename or directory respectively, which are displayed on your form. One drawback is that their usage is less suited for a File - Open menu situation.
As a side note, I had problems once when using an application with the TFileNameEdit when using it through a remote control application on a windows 2000
machine - an access violation where the program worked flawlessly when running
in all other environments.
3. Raize.
The Raize components, in my 2.52 version, have TRzFileListBox,
TRzDirectoryTree, and TRzDirectoryListBox. They latter two partly integrate
directories and drives, but aside from this they do not offer much extra.
4. Dialogs.
Go to the dialogs tab of your Delphi, select the TOpenDialog component, and put it on you form.
Add a Tedit component and a button, and paste the following code in the onclick event of the button:
OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
If OpenDialog1.Execute then
begin
Edit1.text := OpenDialog1.FileName;
end;
This solution will in many cases be what you are looking for. For a File - Open menu
situation, it is great. Alas, there seems to be no way to select a directory
with these components. Also, I have been unable to implement it as part of a
form.
You can however change the caption by setting the Title property at design-
or runtime. Less well known is that you can also change the caption of the open
button by adding the following line to the OnShow event of the OpenDialog:
SendMessage(GetParent(OpenDialog1.Handle), CDM_SETCONTROLTEXT, 1, Integer(PChar('Convert')));
(Add Commdlg to your uses clause).
5. Fileunit.
In the vcl directory of your Delphi installation, you will find a unit called filectrl.pas.
This unit contains two overloaded functions, which are declared as:
function SelectDirectory(var Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;
function SelectDirectory(const Caption: string; const Root: WideString;
out Directory: string): Boolean; overload;
The first one offers you flexibility in allowing your user to create new directories but uses 'old' win3.1 interface, the second one offers a more modern interface, but no
possibility to create a new directory on the fly.
Examples:
var
dir1 : string;
begin
If SelectDirectory(dir1, [], 0) then
Edit2.text := dir1;
end;
and :
procedure TForm1.Button3Click(Sender: TObject);
var
dir1 : string;
begin
If SelectDirectory('Select your install directory', 'c:', dir1) then
Edit2.text := dir1;
end;
You can download
a demo program with these statements.