arraysdelphistoragetrichedit

Delphi TRichEdit to Array and local storage


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 );

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 ... :(


Solution

  • 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;