Displays a file selection dialog Dialogs unit
  type TOpenDialog;
The TOpenDialog is a visual component. It is used to allow a user to select one or more files to open.
It can be defined by dragging the open dialog icon from the Dialogs tab in Delphi, or by defining a TOpenDialog variable.
The TOpenDialog can be configured to suit your needs. When using it, you would proceed along the following steps:
Creating the dialog object
You define a TOpenDialog variable, and then assign a new TOpenDialog object to it:
  openDialog : TOpenDialog;
  openDialog := TOpenDialog.Create(self);

Note that the dialog must have an anchor - here we provide the current object - self - as the anchor.
Setting options
Before displaying the dialog, you are likely to configure it to your needs by setting the dialog properties. Here are the main properties:
Title property
Used to set the caption for the dialog.
FileName property
Gives a default file name to open. (Otherwise, the file name field is blank). When returning from the dialog, if the user has hit OK, this property will contain the (first) selected file name, including its full path (see the first example).
Filter property
This allows only certain file types to be displayed and selectable. The filter text is displayed in a drop down below the file name field. The following example selects for text files only:
openDialog.Filter := 'Text files only|*.txt';
The drop down dialog shows the description before the | separator. After the separator, you define a mask that selects the files you want.
openDialog.Filter := 'Text and Word files only|*.txt;*.doc';
Above we have allowed two different file types, separated by a ;.
openDialog.Filter := 'Text files|*.txt|Word files|*.doc';
Above we have allowed text and Word files as two options in the drop down list.
FilterIndex property
Defines which (starting at 1) of the drop down filter choices will be displayed first.
InitialDir property
Sets the starting directory in the dialog.
Options property
This is a set of TOpenOptions flags. These are quite extensive. The key values are:
ofReadOnly Opens the file for read only
ofFileMustExist Only existing file may be opened
ofAllowMultiSelect User can select 2 or more files

Displaying the dialog
We now call a method of TOpenDialog:
if openDialog.Execute
then ...

Execute returns true if the user selected a file and hit OK. You can then use the selected file:
Finishing with the dialog
The selected file or files are obtained using the following properties:
FileName property
This holds the full path plus file name of the selected file
Files property
This holds the full path plus file name of the a multiple file selection. The file names are held in the returned TStrings value (see the TStringList for more on string lists).
Finally, we must free the dialog object:;
Related commands
Append Open a text file to allow appending of text to the end
AssignFile Assigns a file handle to a binary or text file
PromptForFileName Shows a dialog allowing the user to select a file
Reset Open a text file for reading, or binary file for read/write
TSaveDialog Displays a dialog for selecting a save file name
TStringList Holds a variable length list of strings
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : Illustrating single file selection
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  openDialog : TOpenDialog;    // Open dialog variable
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);

  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;

  // Only allow existing files to be selected
  openDialog.Options := [ofFileMustExist];

  // Allow only .dpr and .pas files to be selected
  openDialog.Filter :=
    'Delphi project files|*.dpr|Delphi pascal files|*.pas';

  // Select pascal files as the starting filter type
  openDialog.FilterIndex := 2;

  // Display the open file dialog
  if openDialog.Execute
  then ShowMessage('File : '+openDialog.FileName)
  else ShowMessage('Open file was cancelled');

  // Free up the dialog
Hide full unit code
   An open file dialog is displayed with two drop down filter choices:
     Delphi project files
     Delphi pascal files    - this is displayed at the start
   The dialog is positioned to the current directory (which will be
   the Delphi project directory if running the code from within
   If you select a file, such as 'Unit1.pas' then it is displayed
   in the ShowMessage dialog like this:
     File : C:\Program Files\Borland\Delphi7\Projects\Unit1.pas
Example code : Selecting multiple files
  openDialog : TOpenDialog;    // Open dialog variable
  i : Integer;
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);

  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;

  // Allow multiple files to be selected - of any type
  openDialog.Options := [ofAllowMultiSelect];

  // Display the open file dialog
  if not openDialog.Execute
  then ShowMessage('Open file was cancelled')
    // Display the selected file names
    for i := 0 to openDialog.Files.Count-1 do

  // Free up the dialog
Show full unit code
   An open file dialog is displayed. Select a few files using
   mouse drag or CTRL-click.
   When you press OK, the selected files are shown. Like this:
     C:\Program Files\Borland\Delphi7\Projects\Unit1.dcu
     C:\Program Files\Borland\Delphi7\Projects\Unit1.pas
Delphi Programming Neil Moffatt 2002 - 2018. All rights reserved.  |  Home Page