TSaveDialog displays a "Save As" dialog for saving files.
TSaveDialog displays a modal Windows dialog box for selecting file names and saving files. The dialog does not appear at runtime until it is activated by a call to the Execute method. When the user clicks Save, the dialog closes and the selected file name is stored in the FileName property.
Properties and methods
constructor Create(TComponent AOwner);
Creates and initializes a file-selection dialog.
Ex.
TSaveDialog saveDlg = TSaveDialog.Create(form);
property string DefaultExt;
Specifies a default file extension.
Ex.
saveDlg.DefaultExt = ".txt";
property string FileName;
Indicates the name and directory path of the file selected. The FileName property returns the name and complete directory path of the selected file.
property string Filter;
Determines the file masks (filters) available in the dialog.
Ex.
saveDlg.Filter = "Text files (*.txt)|*.TXT|Pascal files (*.pas)|*.PAS";
property int FilterIndex;
Determines which filter is selected by default when the dialog opens. Set FilterIndex to 1 to choose the first file type in the list as the default, or set FilterIndex to 2 to choose the second file type as the default, and so forth.
function bool Execute;
Displays the dialog box. Execute returns true when the user makes a selection and clicks OK, and returns false when the user closes the dialog without making a selection.
Ex.
TSaveDialog saveDlg = TSaveDialog.Create(form);
saveDlg.DefaultExt = ".txt";
saveDlg.Filter = "Text files|*.txt";
if (saveDlg.Execute) {
memo.Lines.SaveToFile(saveDlg.FileName);
}
saveDlg.Free;
procedure Free;
Destroys an object and frees its associated memory, if necessary.
property string InitialDir;
InitialDir determines the default directory displayed in the file-selection dialog when it opens. For example, to point the dialog at the WINDOWS\SYSTEM directory, set the value of InitialDir to C:\WINDOWS\SYSTEM.
property string Title;
Specifies the text in the dialog's title bar.
Usage
TForm f;
TButton btnOpen,btnSave;
TMemo memo;
/////////////////////////////////////
// Open a file dialog and open the
// selected file in the memo.
/////////////////////////////////////
void btnOpenClick(TObject Sender) {
TOpenDialog openDlg = TOpenDialog.Create(f);
openDlg.DefaultExt = ".txt";
openDlg.Filter = "Text files|*.txt";
if (openDlg.Execute) {
memo.Lines.LoadFromFile(openDlg.FileName);
}
openDlg.Free;
}
/////////////////////////////////////
// Open a save as dialog and save the
// text in the memo.
/////////////////////////////////////
void btnSaveClick(TObject Sender) {
TSaveDialog saveDlg = TSaveDialog.Create(f);
saveDlg.DefaultExt = ".txt";
saveDlg.Filter = "Text files|*.txt";
if (saveDlg.Execute) {
memo.Lines.SaveToFile(saveDlg.FileName);
}
saveDlg.Free;
}
// Main procedure
{
// Create a new window (form)
f = new TForm(nil);
f.Caption = "A simple text editor!";
f.Position = poScreenCenter;
f.Width = 400;
f.Height = 300;
// Add an open file button to the window
btnOpen = new TButton(f);
btnOpen.Name = "btnOpen";
btnOpen.Parent = f;
btnOpen.SetBounds(10, 20, 75, 25);
btnOpen.Anchors = akLeft+akTop;
btnOpen.Caption = "Open...";
btnOpen.OnClick = &btnOpenClick;
// Add a file save button to the window
btnSave = new TButton(f);
btnSave.Name = "btnSave";
btnSave.Parent = f;
btnSave.SetBounds(90, 20, 75, 25);
btnSave.Anchors = akLeft+akTop;
btnSave.Caption = "Save As...";
btnSave.OnClick = &btnSaveClick;
// Add a text edit control (memo) to the window
memo = new TMemo(f);
memo.Name = "memoLorem";
memo.Parent = f;
memo.ScrollBars = ssBoth;
memo.SetBounds(10, 50, 380, 240);
memo.Anchors = akLeft+akTop+akRight+akBottom;
memo.Lines.Text = "Lorem Ipsum iaculis audire mi moderatius\r\ncorpora dictumst turpis.";
// Show the window
f.ShowModal;
f.Free;
}
|