Solved

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

Posted on 2006-06-23
6
397 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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