RAD Studio VCL Reference
|
Specify text file encodings.
property Encodings: TStrings;
__property TStrings Encodings;
The Encodings property specifies text file encodings. These encodings are stored into a TStrings object. This inclusion means that every string containing the name of the encoding also has attached a pointer to an object (usually a TEncoding object).
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!
|