Solved

Someone please sort out this code! Update Memo1.Lines Quickly.

Posted on 2006-06-23
6
398 Views
Last Modified: 2011-09-20
Hey guys,

Dunno if you can see my previous questions, but if you can't I'll post what's happening. I'm trying to use this code:
----------------
global variables
ts:TStrings;
    sd:TSaveDialog;
    i:integer;
    s:string;
procedure TForm1.BtnDeleteClick(Sender: TObject);
var
value:string;
userinput: integer;
begin
    if not InputQuery('Enter the length to Delete', 'Enter the length to delete (Must be a number)', value)
    then exit;
    Userinput := strtoint(value);
    ts := Memo1.Lines; ********
    for i:=0 to ts.count-1 do
     begin
      s:=ts.strings[i];
      delete(s,1,UserInput);
      ts.Strings[i]:=s;
     end;
    //Memo1.Lines.Equals(ts)       ;
  //  memo1.Lines.AddStrings(ts);
    Memo1.Lines:=ts; *****
end;
--------------------------

I've put stars to explain. "ts := Memo1.Lines;" If I leave this out then my deletion runs very fast i.e. instantly, but If I delete something manually in the text it will revert back to the original when I click to delete the length.

If I put this in "ts := Memo1.Lines;", the deletion takes a while i.e. goes through each line, and then the Memo1 comes up blank??!?!?!

That's why I put this code in  instead of Memo1.Lines :=ts;

//Memo1.Lines.Equals(ts) ;
  //  memo1.Lines.AddStrings(ts);

Does anyone know how to fix this problem. I want to keep the speed as well as allow the user to edit the Memo manually. I also have the same problem when inserting text.

 if not InputQuery('Add to the Beginning of the Line', 'Enter the characters to add)', Userinput)
    then exit;
     for i:=0 to ts.count-1 do
     begin
      s:=ts.strings[i];
      Insert(UserInput, s,1);
      ts.Strings[i]:=s;
     end;
    Memo1.Lines:=ts;

Please Help. Thanks in advance
0
Comment
Question by:shakoor_h
  • 3
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 16972481

You don't need to use another list, plus you still should consider locking/unlocking the string list to get the performance (using BeginUpdate/EndUpdate).

Regards,
Russell

Eg:

var  value:      String;
     userinput:  Integer;
     s:          String;
     i:          Integer;
begin

  if (InputQuery('Enter the length to Delete', 'Enter the length to delete (Must be a number)', value)) then
  begin
     Userinput:=StrToIntDef(value, 0);
     if (userinput > 0) then
     begin
        Memo1.Lines.BeginUpdate;
        try
           for i:=0 to Pred(Memo1.Lines.Count) do
           begin
              s:=Memo1.Lines[i];
              Delete(s, 1, UserInput);
              Memo1.Lines[i]:=s;
           end;
        finally
           Memo1.Lines.EndUpdate;
        end;
     end;
  end;

end;

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16973724
The probelm you are having in your code is you are setting the pointer ts to the memo strings, rather than actually copying the data, if you wanted to do that, it would be
"ts.asign(memo1.lines);" and you may need to do a "ts := TStringList.Create;" first and then
"memo1.lines.assign(ts);" and a "ts.free;" after
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16973843

Exactly...
And as stated, there really isn't any reason to go to that trouble if you lock / modify / unlock inplace.

Russell
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:shakoor_h
ID: 16975508
Exactly what I wanted. what do you mean by Lock/modify/unlock? I have done a beginners course in delphi only, and am trying to learn through trial and error.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16975682

I meant the use of BeginUpdate/EndUpdate when making changes to a TStrings or TStringsList used by the control. It keeps the list from notifying the control of changes until the EndUpdate is called.

List.BeginUpdate; // Keeps the list notifying changes
try
   List.Clear;               // Example of modifying the list
   List.Add(SomeText); // ...
finally
   List.EndUpdate; // Unlock the list and notify changes
end;

Regards,
Russell
0
 

Author Comment

by:shakoor_h
ID: 16978986
OK. But I have noticed that the text updates faster than before, but not as fast as first i.e. when I didn't assign ts:= memo1.lines.

Do you have any ideas to speed it up/update in the background?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question