Boolean16
Boolean16

Reputation: 19

How to insert and retrive a TStringList in a TObjectList

I need to save/load many TRichEdit.lines objects to/from a TobjectList and of course save/load to/from a stream and pass back it to the RichEdit.

e.g.

ObjList.Add(RichEdit.Lines)
RichEdit.Lines:=TStrings(ObjList[]).....??? 

It does not work in both directions and how to reload all the ObjList from file/stream ? Thank you for help

Upvotes: 0

Views: 741

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

This code looks wrong:

ObjList.Add(RichEdit.Lines)

Here you are simply taking a reference to the TStrings object owned by the rich edit control. Whilst your question title talks about TStringList, there is no instance of TStringList here.

I presume you mean to take a copy of the strings:

var
  Strings: TStringList;
....
Strings := TStringList.Create;
Strings.Assign(RichEdit.Lines);
ObjList.Add(Strings); // assume that ObjList now owns Strings

Then in the other direction, your code is already fine:

RichEdit.Lines := ObjList[index] as TStrings; 
// this actually copies the content rather than taking a reference to the object

But you might want to make the code match that in the other direction:

RichEdit.Lines.Assign(ObjList[index] as TStrings); 

Upvotes: 4

Related Questions