[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How can I delete all empty strings in TRichEdit ?

Posted on 1999-11-12
19
Medium Priority
?
171 Views
Last Modified: 2010-04-04
Can you correct my code:
 Repeat
     Number:=RichEdit1.Lines.Indexof('');
      if Number <> -1 then RichEdit1.Lines.Delete(Number);
 Until Number = -1;

When Delete(Number) has executed 1st time RichEdit1.Lines.Count became equal Number (Why?) and as a result this procedure think that job is over. Where is my mistake?



0
Comment
Question by:Masonok
  • 8
  • 4
  • 4
  • +1
19 Comments
 
LVL 7

Expert Comment

by:RBertora
ID: 2202543
here is how:
Rob ;-)

procedure TForm1.Button1Click(Sender: TObject);
var
  number : integer;
begin
  for number := RichEdit1.Lines.Count-1 downto 0 do
  begin
    if RichEdit1.Lines[number] = '' then
      RichEdit1.Lines.Delete(number);
  end;
end;
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202556
And there is nothing wrong with your function:
procedure TForm1.Button1Click(Sender: TObject);
var
  number : integer;
begin
  Repeat
     Number:=RichEdit1.Lines.Indexof('');
      if Number <> -1 then RichEdit1.Lines.Delete(Number);
  Until Number = -1;
end;

I just tried it, it works! What is your problem then?

Rob ;-)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202623
Care to leave a comment?
Rob;-)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 

Author Comment

by:Masonok
ID: 2202630
I tried my code by myself it works! but not in my program! :)
Your second choice works very good in my program with only one problem - (RichEdit.Lines.Count-1). After DELETE(Number) RichEdit.Lines.Count became equals Number so if there are 2 emptylines together 1 empty line will be missed!

Could you tell me why  - After DELETE(Number) RichEdit.Lines.Count became equals Number????

0
 

Author Comment

by:Masonok
ID: 2202635
Sorry that I didn't write a comments first time :)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202640
>After DELETE(Number) RichEdit.Lines.Count became equals Number so if there are 2 emptylines together 1 empty line will be missed!

-That doesn't happen in my program.




After DELETE(Number) RichEdit.Lines.Count = RichEdit.Lines.Count-1

I don't know why it becomes number on your application.. something very strange is happening on your side.

Rob ;-)
0
 
LVL 1

Expert Comment

by:victor_christov
ID: 2202738
Try this

procedure TForm1.Button1Click(Sender: TObject);
Var Number : Integer ;
begin
  number := 0 ;
  with RichEdit1 do begin
    repeat
        if Trim(Lines[number]) = '' then
           Lines.Delete(number)
        else
           Inc(number) ;
    until number >= Lines.Count;
  end;
end;

It's working slowly your way, but final result is guaranteed.


0
 
LVL 1

Expert Comment

by:victor_christov
ID: 2202743
You have to use Trim() only if you want to delete rows conprised of spaces only

Regards, Victor
0
 
LVL 4

Expert Comment

by:karoo
ID: 2202753
Masonok,

In project options, turn optimization off. Had something similar once with a downto loop. just a stupid idea from me, but like Rob said there is nothing wrong with your code.
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202762
Hey Karoo nice to see you :-)
Just wondering, do you know where the Karoo is?
Rob ;-)
0
 
LVL 4

Expert Comment

by:karoo
ID: 2202783
Rob,
aaaah yes, i'm origionally from South-Africa, the Karoo with it's open bare plains is one of my fav places in the world. your'e taking me back to some fond memories now:-))
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202820
Beaufortwest!
Rob ;-)
0
 
LVL 4

Expert Comment

by:karoo
ID: 2202868
no way!!!:))
my family used to have a farm in the Beaufortwest area, Dad went to school there (he used to say: i had to walk 15 km to school on a dirt road, hehe)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202882
Dis a mooorse lang trap.
Rob ;-)
were you at now?
0
 
LVL 4

Expert Comment

by:karoo
ID: 2202922
haahaha, Ek is in die US of A, consulting, trying to get my Green Card, you?
:)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2202937
Karoo:
UK, hope you get green card, it sickens me when all these illegal buggers get across and good / hard working people are questioned to the nth degree...


Masonok,  Whats happening did turning off the optimizations as Karoo suggested work?

Rob ;-)
0
 

Author Comment

by:Masonok
ID: 2207208
Sorry folks! It doesn't work!
I tried Trim and tried to turned off/on an optimization! The result is SUX! After Delete(number) lines.Count became equal number -

I tried my code in the simplest program - Trichedit component and 1 Button! It works perfectly! I type a text then press the button! One moment and text is cleaned from empty lines!

But in my program it doesn't work! The program is simple - I am loading a text from text-file into TRichEdit1 and then tring to erase empty lines then I save it into the document with the same name!
I don't call any API or something wierd....

I need another suggestions!
0
 
LVL 1

Accepted Solution

by:
victor_christov earned 200 total points
ID: 2207485
Hi Masonok

1.If you load RichEdit form Text-only file, then you have to set
  Plain Text := True ;

2.Try this code

procedure TForm1.Button1Click(Sender: TObject);
Var Number  : Integer ;
    cMyFile : String  ;
begin
  cMyFile := { add your file name }
  with RichEdit1 do begin
       Lines.LoadFromFile(cMyFile);
       Perform(EM_LINESCROLL,0,RichEdit1.Lines.Count-1);
       Number := 0    ;
       Repeat
         if Trim(Lines[Number]) = ''
            Lines.Delete(Number);
         else
            Inc(Number) ;
       until Number >= Lines.Count;
       Lines.SaveToFile(cMyFile);
  end;
end;

 
0
 

Author Comment

by:Masonok
ID: 2207530
Verrrry GOOOD!!!!!!! Excelent!

It works! It even works with RichEdit.Lines.IndexOf('') and then Delete method! :)

But your way works faster 2 or 3 times!!!

Thank you MAN!  
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses

607 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