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:
saveDialog : TSaveDialog;
saveDialog := TSaveDialog.Create(self);
Note that the dialog must have an anchor - here we provide the current object - self - as the anchor.
Before displaying the dialog, you are likely to configure it to your needs by setting the dialog properties. Here are the main properties:
Gives the caption to the dialog.
Gives a default file name to save. (Otherwise, the file name field is blank).
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!
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.
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
then ShowMessage('File : '+saveDialog.FileName)
else ShowMessage('Save file was cancelled'); // Free up the dialog