0% found this document useful (0 votes)
150 views2 pages

Delphi Code for Multi-File Selection

The document provides example code for selecting multiple files using an open file dialog in Delphi. An open file dialog is displayed that allows the user to select multiple files using drag selection or CTRL-click. When the user clicks OK, the names of the selected files are displayed in message boxes. The code demonstrates using the TOpenDialog component to select multiple files and access the selected files through the Files property of the dialog.

Uploaded by

alibabavl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views2 pages

Delphi Code for Multi-File Selection

The document provides example code for selecting multiple files using an open file dialog in Delphi. An open file dialog is displayed that allows the user to select multiple files using drag selection or CTRL-click. When the user clicks OK, the names of the selected files are displayed in message boxes. The code demonstrates using the TOpenDialog component to select multiple files and access the selected files through the Files property of the dialog.

Uploaded by

alibabavl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Example code : Selecting multiple files

// 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;

interface

uses
SysUtils,
Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

procedure TForm1.FormCreate(Sender: TObject);


var
openDialog : topendialog; // Open dialog variable
i : Integer;
begin
// 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')
else
begin
// Display the selected file names
for i := 0 to openDialog.Files.Count-1 do
ShowMessage(openDialog.Files[i]);
end;

// Free up the dialog


openDialog.Free;
end;

end.
Hide 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

Dynamic arrays

Dynamic arrays do not have their size defined in their declaration:

var
wishes : array of string; // No size given
begin
SetLength(wishes, 3); // Set the capacity to 3 elements
end;

Here we have defined a wishes array containing string elements. We use the SetLength routine (click on
it to find out more) to set the array size. Such arrays are called dynamic because their size is determined
dynamically (at run time). The SetLength routine can be used to change the array size more than once -
decresaing or increasing the size as desired. My wishes array (list) may indeed grow quite large over time.

Note that we have not given the starting index of the array. This is because we cannot - dynamic arrays
always start at index 0.

Open arrays to routines

This is a more specialised use. Open array parameters allow a routine to receive an array of unknown
number of dimensions. Delphi silently passes the size to the routine as a hidden parameter. Full example
code can be found in Array.

You might also like