Solved

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

Posted on 2006-06-23
6
396 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now