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.
Creating the dialog object
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 Filter
saveDialog.Filter := 'Text files only|*.txt'; (for *.txt save fileonly)
saveDialog.Filter := 'Text files|*.txt|Word files|*.doc'; ((for *.txt, *.doc save file))
Here’s the full source code:
//******************************************//
// Sampel Source Code Learning Delphi //
// SaveDialog //
// by : G. Giarto //
// E-Mail : support@learning-delphi.com //
// Website: http://www.learning-delphi.com //
//******************************************//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
dlgSave1: TSaveDialog;
btn1: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := 'Save Dialog [Simple Sample]';
mmo1.Clear;
end;
procedure TForm1.btn1Click(Sender: TObject);
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 Dialog [Simple Sample] to 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
begin
ShowMessage('File : '+saveDialog.FileName);
mmo1.Lines.SaveToFile(saveDialog.FileName);
end
else ShowMessage('Save file was cancelled');
// Free up the dialog
saveDialog.Free;
end;
end.
After the run will appear as follows:
Results files are stored as follows:
May be useful
-=Thank.you=- How To Make Simple Save Dialog with Borland Delphi 7
0 comments:
Post a Comment