• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

Assigning TRichedit.Lines to a TStrings variable

Take a look at this code.
ChatWnd is a TRichEdit.

Var
  Server : TStrings;

Procedure DoIt;
Var
  P : Pointer;
Begin
 Server := TStringList.Create;
 P := @ChatWnd.Lines;
 TStrings(P^) := Server;
End;

This makes the "Server" text go into Both Server and ChatWnd.Lines but the
painting or something of ChatWnd seems to never occur anymore,
I am missing something essential.


Any ideas?


0
raidos
Asked:
raidos
1 Solution
 
simonetCommented:
I don't get it. What do you want to do?

>I am missing something essential.

I think so. The ^ and @ operators are entirely unnecessary there, because all objects are pointers.

Assigning an object to an untyped pointer goes like this:

P := pointer(ChatWnd.lines);


What I don't get it:

in the second line, you are assigning a value to P. Then, on the second line, you again assign a new value to P, losing the previously assigned value.

What exactly are you trying to achieve?

If all you want is to copy ChatWnd.lines to Server, you can do:

Server := TStringList.create;
Server.assign(ChatWnd.lines);


That will do the job, if that's what you want.

Alex

0
 
xsoftCommented:
If Alex's answer is not what you are looking for, then please tell us what the problem is.
0
 
raidosAuthor Commented:
Ok, This is what i want to do

I have an array of TStrings that will contain text,
when i have Activate one of the TStrings then The text previously sent and Sent while TStrings object is active should come up in The RichEdit

IE:
 Something like this:
 Server := TStringList.create;
 ChatWnd.Lines.assign(Server);

 With autoupdate of
   ChatWnd.Lines from Server

Sorry about the mess...
I have to little experience to try and do something like this.

Bahhh...
I hope this will clarify a few of my thoughts and intentions.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EdHillmannCommented:
I think I'm tracking what you want to do.... something like this...

var
  //I've defined the array of as dynamic... do you plan on a fixed-size array, or have unlimited StringLists in the array?
  aTextArray: array of TStrings;

....

{
Procedure to populate the RichEdit control from the Stringlist in the array.  Use the parameter passed into the function as the position in the array.
}
procedure UseStringHistory(i: integer; pRichEdit: TRichEdit)
var
  vStringList: TStrings;
begin
  vStringList := aTextArray[i;

  pRichEdit.Lines.Assign(vStringList);
end;

{
Procedure to update the String Array from the RichEdit control.  Will also maintain the size of the array as well as ensure that a string list exists in the position...
}
procedure UpdateStringHistory(i: integer; pRichEdit: TRichEdit);
var
  vList: TStrings;
begin
  if (i >= Length(aTextArray)) then
    SetLength(aTextArray, i + i);

  vList := aTextArray[i];
  if not assigned(vList) then begin
    vList := TStringList.Create;
    aTextArray[i] := vList;
  end;

  vList.Assign(pRichEdit.Lines);
end;

You also want to make sure that the array of stringlists gets freed up at the appropriate time.  You could do it when the form is destroyed if they end up being private variables on the form (probably the safest route).  Or on the finalization of the unit containing the array. if it is to remain a implementation variable.  Wherever it sits, it can look like...

var
  i: integer;
begin
  for i := Length(aTextArray) - 1 do begin
    if Assigned(aTextArray[i]) then
      aTextArray[i].Free;
  end;
end;

Hope this helps,
Ed
0
 
LuKCommented:
Just do it like you had descibed it in your first question, but use
TStrings(p^) := Server.STRINGS[0];
instead of
TStrings(p^) := Server;

0
 
raidosAuthor Commented:
I have just returned from a trip, i will evaluate your comments tonight.

sorry about the delay(zzzzzzzZZZZzz);


//raidos
0
 
raidosAuthor Commented:
Thank you for your comments,
I think i will have to go with Eds comment although it's not exactly what I asked for.

If any of you disagree with that, please respond before 10 AM(GMT) tomorrow.

Thanks again

//raidos

0
 
raidosAuthor Commented:
Comment accepted as answer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now