Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-23
6
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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