| View previous topic :: View next topic |
| Author |
Message |
Oren Guest
|
Posted: Mon Apr 26, 2004 4:32 pm Post subject: RichEdit1->Lines->Delete(x) doesn't delete anything at once. |
|
|
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
|
Posted: Mon Apr 26, 2004 5:15 pm Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o |
|
|
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
|
Posted: Mon Apr 26, 2004 5:23 pm Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o |
|
|
Oren wrote:
| Quote: | RichEdit1->Lines->Delete(x);
|
RichEdit1->Lines->Delete(x--);
Hans.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Apr 26, 2004 6:44 pm Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o |
|
|
"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
|
Posted: Mon Apr 26, 2004 9:56 pm Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o |
|
|
thank you very much guys !
now it works ;-)
Oren
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Apr 27, 2004 7:54 am Post subject: Re: RichEdit1->Lines->Delete(x) doesn't delete anything at o |
|
|
Oren Halvani wrote:
| Quote: | thank you very much guys !
now it works
|
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 |
|
 |
|