Solved

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

Posted on 2006-06-23
6
394 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
Comment Utility

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
Comment Utility
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
Comment Utility

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

Russell
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:shakoor_h
Comment Utility
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
Comment Utility

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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

9 Experts available now in Live!

Get 1:1 Help Now