BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RichEdit1->Lines->Delete(x) doesn't delete anything at once.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Oren
Guest





PostPosted: Mon Apr 26, 2004 4:32 pm    Post subject: RichEdit1->Lines->Delete(x) doesn't delete anything at once. Reply with quote



hi dear builders,

i want to delete lines in a TRichEdit that contain a string like: "oren"
the below function works, but i have to click several times on the button to
delete ALL "oren" out of the TRichEdit...whats the problem why..?

I set: RichEdit1->WordWrap = true

is there maybe a nicer way as:

if(RichEdit1->Lines->Strings[x].AnsiPos("oren") > 0)


thanks for any help on this...

Oren


/**********************************************/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int x = 0; x < RichEdit1->Lines->Count; x++)
{
if(RichEdit1->Lines->Strings[x].AnsiPos("oren") > 0)
{
RichEdit1->Lines->Delete(x);
}
}
}
/**********************************************/


Back to top
James Cammarata
Guest





PostPosted: Mon Apr 26, 2004 5:15 pm    Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o Reply with quote



One problem you're probably having is that when you delete from the string
list, the Count and current position also change. Your for loop increments
x all the time, and when you delete something it is like skipping 2 lines
ahead.

Do this instead:

int x = 0;
while(x < RichEdit1->Lines->Count) {
if(RichEdit1->Lines->Strings[x].AnsiPos(searchString) > 0) {
RichEdit1->Lines->Delete(x);
}
else {
x ++;
}
}

This way, x is only incremented if something is not deleted from the string
list.


Back to top
Hans Galema
Guest





PostPosted: Mon Apr 26, 2004 5:23 pm    Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o Reply with quote



Oren wrote:


Quote:
RichEdit1->Lines->Delete(x);


RichEdit1->Lines->Delete(x--);

Hans.

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 26, 2004 6:44 pm    Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o Reply with quote

"Oren" <sharon78 (AT) gmx (DOT) de> wrote


Quote:
for(int x = 0; x < RichEdit1->Lines->Count; x++)

You are looping in the wrong direction. Every time you delete a line, you
are effecting your loop counter, that is why you are skipping lines and have
to run the code multiple times to get everything. You need to loop
backwards, not forwards:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
RichEdit1->Lines->BeginUpdate();
try
{
for(int x = RichEdit1->Lines->Count-1; x >= 0; --x)
{
if( RichEdit1->Lines->Strings[x].Pos("oren") > 0 )
RichEdit1->Lines->Delete(x);
}
}
__finally {
RichEdit1->Lines->EndUpdate();
}
}


Gambit



Back to top
Oren Halvani
Guest





PostPosted: Mon Apr 26, 2004 9:56 pm    Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o Reply with quote

thank you very much guys !
now it works ;-)


Oren


Back to top
Hans Galema
Guest





PostPosted: Tue Apr 27, 2004 7:54 am    Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o Reply with quote

Oren Halvani wrote:

Quote:
thank you very much guys !
now it works Wink

Your post had a very confusing Subject:

RichEdit1->Lines->Delete(x) doesn't delete anything at once...

but you meant:

RichEdit1->Lines->Delete(x) doesn't delete all at once...

Hans.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.