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 

RichEdit->Text, Delete Lines that contains string "oren" (x)

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





PostPosted: Tue Jun 22, 2004 12:27 pm    Post subject: RichEdit->Text, Delete Lines that contains string "oren" (x) Reply with quote



hi dear builders,

i would like to delete lines in a RichEdit->Text that contains the string
"oren" (x) times..
so, i wrote this:

/****************************************************/
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
int howmuch = 0;

for(int x = re->Lines->Count - 1; x >= 0; --x)
{
if(re->Lines->Strings[x].Pos(txtContains->Text) > 0)
{
howmuch++;
// HowMuch = TEdit...
if(howmuch == HowMuch->Text) {re->Lines->Delete(x);}
}
}
}
/****************************************************/

re->Text is:
.........................xxxxxxxx............................
fgh oren oren oren xgfhdfghf // ONLY THIS LINE WILL BE DELETED..
fgh___oren....oren__oren xgfhdfghf
fgh#######oren####oren##oren##fhdfghf
......................xxxxxxxxx........................

what do i need to modify, that ALL lines that containing "oren" 3 times
will be deleted..?


Oren


Back to top
Liz Albin
Guest





PostPosted: Tue Jun 22, 2004 12:37 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote



On Tue, 22 Jun 2004 14:27:44 +0200, Oren Halvani wrote:

Quote:
what do i need to modify, that ALL lines that containing "oren" 3 times
will be deleted..?

you need to reset howmuch on each line
--
good luck,
liz

Back to top
Hans Galema
Guest





PostPosted: Tue Jun 22, 2004 12:46 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote



Oren Halvani wrote:

Quote:
........................xxxxxxxx............................
fgh oren oren oren xgfhdfghf // ONLY THIS LINE WILL BE DELETED..
fgh___oren....oren__oren xgfhdfghf
fgh#######oren####oren##oren##fhdfghf
.....................xxxxxxxxx........................

what do i need to modify, that ALL lines that containing "oren" 3 times
will be deleted..?

For what you have now: the reason that only that line will be deleted is not
that it contains 3x oren. To see for yourself try:

fgh oren xgfhdfghf // ONLY THIS LINE WILL BE DELETED..
fgh___oren.... xgfhdfghf
fgh#######oren######fhdfghf

The third occurrence of oren will be found if you specify 3 in HowMuch->Text.

You should investigate line by line on the occurrence of oren for the
requested three times.

Hans.

Back to top
Oren Halvani
Guest





PostPosted: Tue Jun 22, 2004 1:08 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote


"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> schrieb im Newsbeitrag
news:40d829ec (AT) newsgroups (DOT) borland.com...

Quote:
You should investigate line by line on the occurrence of oren for the
requested three times.

Hans.

OK, i'm counting now for "oren", if Count = 3 than delete the line...

/*********************************************************/
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
TSearchTypes Options = TSearchTypes() << stMatchCase;

AnsiString SearchString = txtContains->Text;

int Pos = - SearchString.Length(),
Count = 0;

for(int x = re->Lines->Count - 1; x >= 0; --x)
{
if(re->Lines->Strings[x].Pos(txtContains->Text) > 0)
{
// temp RichEdit for function FindText(..)
TRichEdit* pRichEdit = new TRichEdit(this);
pRichEdit->Visible = false;
pRichEdit->Parent = this;
pRichEdit->Text = re->Text;

while((Pos = pRichEdit->FindText(SearchString, Pos +
SearchString.Length(), pRichEdit->Text.Length(), Options)) != -1)
Count++;

delete pRichEdit;
}
if(Count == HowMuch->Text) {re->Lines->Delete(x);}
}
}
/*********************************************************/

nothing happens now...


Oren



Back to top
Hans Galema
Guest





PostPosted: Tue Jun 22, 2004 1:43 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote

Oren Halvani wrote:

Quote:
if(Count == HowMuch->Text) {re->Lines->Delete(x);}

nothing happens now...

Take a TLabel and let it display Count.

Label1->Caption = IntToStr ( Count );]
Label1->Update ();

Hans.

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Jun 22, 2004 5:33 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote


"Oren Halvani" <NoSpam (AT) fdtd (DOT) com> wrote


Quote:
so, i wrote this:

Try this instead:

void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
AnsiString ToFind = txtContains->Text;
int Matches = HowMuch->Text.ToInt();
TSearchTypes Types = TSearchTypes() << stWholeWord;

for(int x = re->Lines->Count - 1; x >= 0; --x)
{
int howmuch = 0;

int start = re->Perform(EM_LINEINDEX, x, 0);
int length = re->Perform(EM_LINELENGTH, start, 0);
int idx;

do
{
idx = re->FindText(ToFind, start, length, Types);
if( idx > -1 )
{
++howmuch;
start = idx + ToFind.Length();
length -= start;
}
}
while( (idx > -1) && (length > 0) );

if( howmuch == Matches )
re->Lines->Delete(x);
}
}


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Jun 22, 2004 5:34 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote


"Oren Halvani" <NoSpam (AT) fdtd (DOT) com> wrote


Quote:
TRichEdit* pRichEdit = new TRichEdit(this);

Why are you using a temporary TRichEdit? It is not needed, you can use
FindText() on the original TRichEdit directly. See my other reply for an
example.


Gambit



Back to top
Oren Halvani
Guest





PostPosted: Tue Jun 22, 2004 9:58 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote

hi Remy, seems there are some typos..the function doesn't do anything :-(

txtContains->Text = "oren"
HowMuch->Text = 3
re->Text =
-------------------------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
fgh oren oren oren xgfhdfghf
fgh___oren....oren__oren xgfhdfghf
fgh####oren####oren##oren##fhdfghf
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-------------------------------------------------------

hmm, strange..whats wrong..?

the only thing i've changed is stWholeWord to stMatchCase...

/*************************************************/
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
AnsiString ToFind = txtContains->Text;
int Matches = HowMuch->Text.ToInt();
TSearchTypes Types = TSearchTypes() << stMatchCase;

for(int x = re->Lines->Count - 1; x >= 0; --x)
{
int idx,
howmuch = 0,
start = re->Perform(EM_LINEINDEX, x, 0),
length = re->Perform(EM_LINELENGTH, start, 0);

do
{
idx = re->FindText(ToFind, start, length, Types);
if(idx > -1)
{
++howmuch;
start = idx + ToFind.Length();
length -= start;
}
}
while((idx > -1) && (length > 0));
if(howmuch == Matches)
re->Lines->Delete(x);
}
}
/*************************************************/

Oren


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Jun 22, 2004 10:14 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote

"Oren Halvani" <NoSpam (AT) fdtd (DOT) com> wrote


Quote:
hi Remy, seems there are some typos..the function doesn't do anything Sad

Try this:

AnsiString ToFind = txtContains->Text;
int Matches = HowMuch->Text.ToInt();
TSearchTypes Types = TSearchTypes() << stWholeWord;

for(int x = re->Lines->Count - 1; x >= 0; --x)
{
int howmuch = 0;
int start = re->Perform(EM_LINEINDEX, x, 0);
int end = start + re->Perform(EM_LINELENGTH, start, 0);
int idx;

while( start < end )
{
idx = re->FindText(ToFind, start, end-start, Types);
if( idx == -1 )
break;
++howmuch;
start = idx + ToFind.Length();
}

if( howmuch == Matches )
re->Lines->Delete(x);
}


Gambit



Back to top
Oren Halvani
Guest





PostPosted: Tue Jun 22, 2004 10:30 pm    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote

works fantastic :-)


thanks Remy


Back to top
Hans Galema
Guest





PostPosted: Wed Jun 23, 2004 5:00 am    Post subject: Re: RichEdit->Text, Delete Lines that contains string "oren" Reply with quote

Oren Halvani wrote:
Quote:
works fantastic Smile

And as soon as it works start optimizing.

Quote:
while( start < end )
{
idx = re->FindText(ToFind, start, end-start, Types);
if( idx == -1 )
break;
++howmuch;

I you are looking for three occurrences and you did find four
then you can stop.

if ( howmuch > Matches )
break;

Quote:
start = idx + ToFind.Length();
}

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.