Delphi TRichEdit to Array and local storage

540 views Asked by At

I'm looking for informations on how to put TRichEdit into an Array and save it to Local file (ex: File.dat). The goal is to store a number of text, with a description, and the 'name' of it.

I think I have to start with:

type
   TMessage = record
     Name : string;
     Desc : string;
     Text : TMemoryStream;
end;

var ARListMessages: array[1..50] of TMessage 

And add data with something like:

richedit.Lines.SaveToStream( ARListMessages[i].Text );
  • How to create the Array, and make manipulations on it (Add, remove ...) with the 'name'?
  • How can I save it (Array), and load it easily from local storage ? (Ex: File.dat)

I've found some informations here, without beeing able to make something working.

Thanks for your time.

[EDIT 18/09/2017]

I'm Still looking to find a solution, and try to find a way to save my informations to a local file.

My actual code to test is :

var
  MessageArray  : array of TMessage;

// // // //

  SetLength(MessageArray, 1);
  MessageArray[0].Name := 'Hey You';
  MessageArray[0].Desc := 'Im here and will stay here, just in case';
  MessageArray[0].Text := TMemoryStream.Create;
  MessageArray[0].Text.Position := 0;
  RichEdit1.plaintext := false;
  RichEdit1.Lines.SaveToStream( MessageArray[0].Text );

So, looking to save MessageArray, but haven't find how to do that yet. I've take a look on SuperObjet, but can't find how to deal with it. Omxl was looking Great and easy, but free trial ... :(

2

There are 2 answers

0
Nasreddine Galfout On BEST ANSWER

How to create the Array, and make manipulations on it (Add, remove ...) with the 'name'?

From your first code the array is already created. No further work is needed. And even if you use dynamic arrays you still don't have to do any thing just declaring it will suffice.

If you are asking "How to create the array ?" as in "How to create such a list, so I could add, remove with the 'name' field" then I would suggest TDictionary (instead of the city names use your field 'name' as a key) to do the manipulations and then when saving use this

How can I save it (Array), and load it easily from local storage ? (Ex: File.dat)

You can't directly just like array.savetofile;.You need to use one of file handling methods like TFileStream to store the array.

Note: in your edit the memory stream will cause you only trouble because each time you create it you will need to free it after its use instead use these functions to extract formatted text and change the field Text : TMemoryStream; to Text : string;

  1. RichTextToStr: Convert the rich format text to a string.

    function RichTextToStr(RichEdit:TRichEdit):string;
    var
    SS : TStringStream;
    begin
    SS := TStringStream.Create('');
      try
      RichEdit.Lines.SaveToStream(SS);
      Result := SS.DataString;
      finally
      SS.Free;
      end;
    end;
    
  2. loadToRichedit: Load the formatted text to the RichEdit again.

    procedure loadToRichedit(Const St:string;RichEdit:TRichEdit);
    var
    SS : TStringStream;
    begin
    SS := TStringStream.Create(St);
       try
       RichEdit.Lines.LoadFromStream(SS);
       finally
       SS.Free;
       end;
    end;
    
1
benda On

I've been able to have an answer. I share it here if someone need the solution.

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.Generics.Collections, Classes, System.Zip;


type
  TPersonne = class
  strict private
     FNom   : string;
     FPrenom: string;
  public
    property Nom:    string read FNom    write FNom;
    property Prenom: string read FPrenom write FPrenom;
  end;

  TFamille = class(TDictionary<string, TPersonne>)
  private
    procedure LoadFromStream(stream: TStream);
    procedure SaveToStream(stream: TStream);
  public
    procedure LoadFromFile(const AFileName: string);
    procedure SaveToFile(const AFileName: string);
  end;

procedure TFamille.SaveToStream(stream: TStream);
var
   i: Integer;
   Personne: TPersonne;
   Pair: System.Generics.Collections.TPair<string, TPersonne>;
   Writer: TWriter;
begin
   Writer := TWriter.Create(stream, 4096);
   try
      Writer.WriteListBegin;
      for Pair in Self do
      begin
         Writer.WriteString(Pair.Key);
         Writer.WriteListBegin;

         Personne := Pair.Value;

         Writer.WriteString(Personne.Nom);
         Writer.WriteString(Personne.Prenom);

         Writer.WriteListEnd;
      end;
      Writer.WriteListEnd;
   finally
      Writer.Free;
   end;
end;

procedure TFamille.LoadFromStream(stream: TStream);
var
   Personne: TPersonne;
   Reader: TReader;
   sKey: string;
begin
   Clear;
   Reader := TReader.Create(stream, 1024);
   try
      Reader.ReadListBegin;
      while not Reader.EndOfList do
      begin
         sKey     := Reader.ReadString;
         Personne := TPersonne.Create;

         Reader.ReadListBegin;

         while not Reader.EndOfList do
         begin
            Personne.Nom    := Reader.ReadString;
            Personne.Prenom := Reader.ReadString;
         end;

         Reader.ReadListEnd;
         Add(sKey, Personne);
      end;
      Reader.ReadListEnd;
   finally
      Reader.Free;
   end;
end;

procedure TFamille.LoadFromFile(const AFileName: string);
var
   Stream: TStream;
begin
   Stream := TFileStream.Create(AFileName, fmOpenRead);
   try
      LoadFromStream(Stream);
   finally
      Stream.Free;
   end;
end;

procedure TFamille.SaveToFile(const AFileName: string);
var
   stream: TStream;
begin
   stream := TFileStream.Create(AFileName, fmCreate);
   try
      SaveToStream(stream);
   finally
      stream.Free;
   end;
end;

var
   Famille:  TFamille;
   Personne: TPersonne;
begin
   try
      Famille := TFamille.Create;
      try
         Personne := TPersonne.Create;
         Personne.Nom    := 'Julie';
         Personne.Prenom := 'Albertine';

         Famille.Add('1', Personne);

         Famille.SaveToFile('D:\famille.txt');

      finally
         FreeAndNil(Famille);
      end;

      Famille := TFamille.Create;
      try
         Famille.LoadFromFile('D:\famille.txt');

         if Famille.Count > 0 then
            Writeln(Famille['1'].Nom + ' ' + Famille['1'].Prenom);
      finally
         FreeAndNil(Famille);
      end;

      Readln;
   except
   on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
   end;
end.

Thanks all for your Help !