RAD Studio VCL Reference
|
TSaveTextFileDialog displays a file-selection dialog.
TSaveTextFileDialog = class(TOpenTextFileDialog);
class TSaveTextFileDialog : public TOpenTextFileDialog;
TSaveTextFileDialog displays a modal Windows dialog box for selecting and opening text files. The dialog does not appear at runtime until it is activated by a call to the Execute method. When the user clicks Open, the dialog closes and the selected files are stored in the Files property.
TSaveTextFileDialog allows the user to select the encoding specific to the text file that is to be saved. This is done by adding encoding items in string form into Encodings and accessing these items by using the EncodingIndex.
C++ Examples:
/* This example shows how to use the TOpenTextFileDialog and TSaveTextFileDialog to handle files with specific encodings. */ void __fastcall TForm1::FormCreate(TObject *Sender) { openTxtDlg = new TOpenTextFileDialog(this); saveTxtDlg = new TSaveTextFileDialog(this); // create a new string list that holds encoding info TStrings* encodings = new TStringList(); // add some encodings to the list encodings->AddObject("ASCII", TEncoding::ASCII); encodings->AddObject("UNICODE", TEncoding::Unicode); encodings->AddObject("UTF8", TEncoding::UTF8); openTxtDlg->Encodings->Assign(encodings); saveTxtDlg->Encodings->Assign(encodings); openTxtDlg->Filter = "Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*"; saveTxtDlg->Filter = "Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*"; } void __fastcall TForm1::Button1Click(TObject *Sender) { if (openTxtDlg->Execute(this->Handle)) { // selecting the filename and encoding selected by the user String filename = openTxtDlg->FileName; int encIndex = openTxtDlg->EncodingIndex; TEncoding* enc = dynamic_cast<TEncoding*> (openTxtDlg->Encodings->Objects[encIndex]); // checking if the file exists if (FileExists(filename)) { // display the contents in a memo based on the selected encoding Memo1->Lines->LoadFromFile(filename, enc); } else { MessageDlg("File does not exist", mtError, TMsgDlgButtons() << mbOK, 0); } } } void __fastcall TForm1::Button2Click(TObject *Sender) { if (saveTxtDlg->Execute(this->Handle)) { // selecting the filename and encoding selected by the user String filename = saveTxtDlg->FileName; int encIndex = saveTxtDlg->EncodingIndex; TEncoding* enc = dynamic_cast<TEncoding*> (saveTxtDlg->Encodings->Objects[encIndex]); // checking if the file exists if (FileExists(filename)) { MessageDlg("File already exists", mtError, TMsgDlgButtons() << mbOK, 0); } else { // saves to file based on the selected encoding. Memo1->Lines->SaveToFile(filename, enc); } } }
Delphi Examples:
{ This example shows how to use the TOpenTextFileDialog and TSaveTextFileDialog to handle files with specific encodings. } procedure TForm1.Button1Click(Sender: TObject); var Encoding : TEncoding; EncIndex : Integer; Filename : String; begin if OpenTxtDlg.Execute(Self.Handle) then begin //selecting the filename and encoding selected by the user Filename := OpenTxtDlg.FileName; EncIndex := OpenTxtDlg.EncodingIndex; Encoding := OpenTxtDlg.Encodings.Objects[EncIndex] as TEncoding; //checking if the file exists if FileExists(Filename) then //display the contents in a memo based on the selected encoding Memo1.Lines.LoadFromFile(FileName, Encoding) else raise Exception.Create('File does not exist.'); end; end; procedure TForm1.Button2Click(Sender: TObject); var Encoding : TEncoding; EncIndex : Integer; Filename : String; begin if SaveTxtDlg.Execute(Self.Handle) then begin //selecting the filename and encoding selected by the user Filename := SaveTxtDlg.FileName; EncIndex := SaveTxtDlg.EncodingIndex; Encoding := SaveTxtDlg.Encodings.Objects[EncIndex] as TEncoding; //checking if the file exists if FileExists(Filename) then raise Exception.Create('File already exists.') else //save to file based on the selected encoding Memo1.Lines.LoadFromFile(FileName, Encoding); end; end; procedure TForm1.FormCreate(Sender: TObject); var encodings : TStrings; begin OpenTxtDlg := TOpenTextFileDialog.Create(Self); SaveTxtDlg := TSaveTextFileDialog.Create(Self); // create a new string list that holds encoding info encodings := TStringList.Create(); // add some encodings to the list encodings.AddObject('ASCII', TEncoding.ASCII); encodings.AddObject('UNICODE', TEncoding.Unicode); encodings.AddObject('UTF8', TEncoding.UTF8); OpenTxtDlg.Encodings.Assign(encodings); SaveTxtDlg.Encodings.Assign(encodings); OpenTxtDlg.Filter := 'Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*'; SaveTxtDlg.Filter := 'Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*'; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|