whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

90 Visitors Online


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

Tip Rating (15):  
     


{
 -> 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 © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners