Kylix
Tips

NEW TIPS
Database (90)
Files (138)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

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 (6):  
     



{
 -> 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