Description
|
The TSaveDialog is a
visual component. It is used to allow a user to
select the name of a file to save to.
It can be defined by dragging the save dialog icon
from the Dialogs tab in Delphi, or by defining a
TSaveDialog variable.
The TSaveDialog can
be configured to suit your needs. When using it,
you would proceed along the following steps:
Creating the dialog
object
You define a TSaveDialog variable, and then assign
a new TSaveDialog object to it:
var
saveDialog : TSaveDialog;
begin
saveDialog :=
TSaveDialog.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
Gives the caption to the dialog.
FileName
property
Gives a default file name to save. (Otherwise, the
file name field is blank).
DefaultExt
property
Defines the extension that will be added to the
user file name, if manually typed (rather than
selected from the file list). If their are two or
more save filter extension types, then this value
is ignored. However, it must be provided in order
for the drop down list extension values to be used.
Strange!
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:
saveDialog.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.
saveDialog.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.
Displaying the
dialog
We now call a method of TSaveDialog:
if
saveDialog.Execute
then ...
Execute returns
true if the user selected a file
and hit OK. You can then save to
the selected file:
Finishing with the
dialog
The selected file obtained using the following
property:
FileName
property
This holds the full path plus file name of the
selected file. Finally, we must free the dialog
object:
saveDialog.free;
|
|
Notes
|
It is your responsibility to detect whether the
user has selected an existing file - you will want
to ask the user if the file should be
overriden.
|
|
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
|
TOpenDialog
|
|
Displays a file selection dialog
|
|
|
|
|
Example code :
|
var
saveDialog : TSaveDialog; //
Save dialog variable
begin
// Create
the save dialog object - assign to our save dialog
variable
saveDialog :=
TSaveDialog.Create(self);
// Give
the dialog a title
saveDialog.Title := 'Save your text or
word file';
// Set up
the starting directory to be the current
one
saveDialog.InitialDir :=
GetCurrentDir;
// Allow
only .txt and .doc file types to be
saved
saveDialog.Filter := 'Text
file|*.txt|Word file|*.doc';
// Set
the default extension
saveDialog.DefaultExt := 'txt';
// Select
text files as the starting filter
type
saveDialog.FilterIndex := 1;
//
Display the open file dialog
if saveDialog.Execute
then WriteLn('File :
'+saveDialog.FileName)
else WriteLn('Save file was
cancelled');
// Free
up the dialog
saveDialog.Free;
end;
|
Show full unit code
|
An save 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
Delphi).
If you select a file, such as
'Unit1.pas' then it is displayed
in the ShowMessage dialog like
this:
File : C:Program
FilesBorlandDelphi7ProjectsUnit1.pas
|
|