Kylix
Tips

NEW TIPS
Database (98)
Files (1187)
Forms (125)
Graphic (126)
IDE (30)
Indy (13)
Internet / LAN (146)
IntraWeb (12)
Math (84)
Misc (137)
Multimedia (56)
Objects/
ActiveX (64)

OpenTools API (14)
Printing (47)
Strings (94)
System (275)
VCL (246)

Search Tip
Top15
Add new Tip

Forum

...saving several controls to one single file?
Author: P. Below
Homepage: http://www.teamb.com
[ Print tip ]    

Tip Rating (3):  
     



{
 -> Question:
 How can I store some TRichEdit components and TEdit Components in a single file?

 -> Answer:
 Use a filestream and a reader or writer object.
 These ease the tasks of writing strings to a binary stream and reading them back.
}

{
 -> Frage:
 Wie kann ich den Inhalt einiger TRichEdit und TEdit Komponenten in einer einzigen
 Datei  speichern?

 -> Antwort:
 Verwende einen FileStraem und ein Reader/Writer Objekt.
 Damit kann man leicht strings in einen binären Stream speichern und zurücklesen.
}

// Save routine
// Speichern Routine
procedure SaveEditcontrols(const FileName: stringconst Controls: array of TCustomEdit);
var
  
fs: TFilestream;
  writer: TWriter;
  i: Integer;
  ss: TStringstream;
begin
  
fs := TFilestream.Create(FileName, fmCreate);
  try
    
writer := TWriter.Create(fs, 4096);
    try
      for 
i := Low(Controls) to High(Controls) do
        if 
Controls[i] is TCustomRichedit then 
        begin
          
ss := TStringstream.Create(EmptyStr);
          try
            with 
TRichedit(Controls[i]) do 
            begin
              
Plaintext := False;
              Lines.SaveToStream(ss);
            end;
            writer.WriteString(ss.Datastring);
          finally
            
ss.Free;
          end;
        end
      else
        
writer.WriteString(Controls[i].Text);
    finally
      
writer.Free;
    end;
  finally
    
fs.Free;
  end;
end;

// Load routine
// Lade Routine
procedure LoadEditcontrols(const FileName: stringconst Controls: array of TCustomEdit);
var
  
fs: TFilestream;
  reader: Treader;
  i: Integer;
  ss: TStringstream;
begin
  
fs := TFilestream.Create(FileName, fmOpenread or fmShareDenyWrite);
  try
    
reader := Treader.Create(fs, 4096);
    try
      for 
i := Low(Controls) to High(Controls) do
        if 
Controls[i] is TCustomRichedit then 
        begin
          
ss := TStringstream.Create(reader.ReadString);
          try
            with 
TRichedit(Controls[i]) do 
            begin
              
Plaintext := False;
              Lines.LoadfromStream(ss);
            end;
          finally
            
ss.Free;
          end;
        end
      else
        
Controls[i].Text := reader.ReadString;
    finally
      
reader.Free;
    end;
  finally
    
fs.Free;
  end;
end;

// Example to store 2 TRichEdits and 3 Edit Controls to one file
// Beispiel, um  3 TRichEdits und 3 TEdit Controls in einer Datei zu speichern

procedure TForm1.Button1Click(Sender: TObject);
begin
  
SaveEditControls('C:\temp\temp.dat',
    [richedit1, richedit2, edit1, edit2, edit3]);
end;

Rate this tip:

poor
very good


Copyright  Torry's Delphi Pages Torry's Delphi Pages Maintained by Simon Grossenbacher Notes? Comments? Feel free to send... Copyright 1996-2001