Board index » cppbuilder » Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)


2004-02-06 09:17:55 PM
cppbuilder105
hi dear builders,
can someone help me with optimizing the below code...?
when I first reverse the content of Memo via this:
/***************************************************************/
void __fastcall TfrmStringDetails::cmdText_ReverseClick(TObject *Sender)
{
AnsiString temp = Memo1->Text;
strrev(temp.c_str());
Application->ProcessMessages();
Memo1->Text = temp;
}
/***************************************************************/
and than insert a line number for each line with this:
/***************************************************************/
void __fastcall TfrmStringDetails::cmdLine_NumbersClick(TObject *Sender)
{
for(int i = 0; i < Memo1->Lines->Count; i++)
{
Memo1->Lines->Strings[i] = String(i + 1) + ".) " +
Memo1->Lines->Strings[i];
int LineCount = Memo1->Lines->Count;
Memo1->Lines->Strings[i] = Memo1->Lines->Strings[i] + "\r\n";
if(Memo1->Lines->Count>LineCount)
Memo1->Lines->Delete(i + 1);
}
}
/***************************************************************/
my programm want stop to insert the line numbers...(endless loop...) but why
??
can someone help and take a look on the code...?
Oren
 
 

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

(Oren) wrote:
Quote
can someone help and take a look on the code...?
It is nice that you so soon followed my suggestion as to
hold this conversation in this ng instead of the nativeapi ng.
But you could as well have answered my questions also before
reposting all the same.
Hans.
 

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

Quote
It is nice that you so soon followed my suggestion as to
hold this conversation in this ng instead of the nativeapi ng.

But you could as well have answered my questions also before
reposting all the same.

Hans.
Hans, what do you mean..? the amonut of the lines is still the same..isnt
it...?
for example, the following lines:
__________________________________________
one
two
three
ok, now after string-reversing:
__________________________________________
eerht
owt
eno
the amount doesn't change...or yes..?
I really don't know where the problem is...
Oren
 

{smallsort}

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

(Oren) wrote:
Quote
Hans, what do you mean..? the amonut of the lines is still the same..isnt
it...?

for example, the following lines:
the amount doesn't change...or yes..?
Not if I copy and paste your example in the TMemo.
Heh, then it stays the same.
But if I type
one
two
three
in a memo then no.
There is a difference in \n and \r\n.
I had tried your code with loading Unit1.cpp in the Memo
using LoadFromFile. It's a mess then with much less lines.
Probably with programmatically adding lines it would be
ok if there is only a \n.
But you keep your problem that the lineend are incorrect now.
Why didnot you tell that ?
There stays your other function
where you add numbers. After one statement you would be ready.
Why introducing a LineCount and adding "\r\n"? YOu did not tell
that you were going to do that. Please explain.
Hans.
 

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

"(Oren)" < XXXX@XXXXX.COM >wrote in message
Quote
my programm want stop to insert the line numbers...(endless loop...)
but why ??
Because you are messing up your line feeds for existing lines, and also
introducing additional line feeds by hand, thus you are effecting the line
count during each iteration such that the loop can never reach the end of
the lines.
If all you are trying to do is swap the order of the lines and also reverse
the strings of each line, then use this code instead:
void __fastcall TfrmStringDetails::cmdText_ReverseClick(TObject *Sender)
{
int LineCount = Memo1->Lines->Count;
int LoopCount = (LineCount / 2) + (LineCount % 2);
int Line1, Line2;
TMemo1->Lines->BeginUpdate();
try
{
for(Line1 = 0; Line1 < LoopCount; ++Line1)
{
Line2 = (LineCount-Line1-1);
Memo1->Lines->Strings[Line1] =
strrev(Memo1->Lines->Strings[Line1].c_str());
if( Line1 < Line2 )
{
Memo1->Lines->Strings[Line2] =
strrev(Memo1->Lines->Strings[Line2].c_str());
Memo1->Lines->Exchange(Line1, Line2);
}
}
}
__finally {
Memo1->Lines->EndUpdate();
}
}
Quote
and than insert a line number for each line with this:
If you are using the line numbers for display purposes only, then a better
approach would be to use the EM_SETRECT message to shrink the Memo's text
area to leave an empty space along the left edge and then you can
custom-draw line numbers in that area so that you are not altering the
actual text at all. Have a look at the following post for details (he used
a TRichEdit, but it applies to TMemo as well):
tinyurl.com/2vlty
aka:
groups.google.com/groups&lr=&ie=UTF-8&oe=UTF-8&selm=38D000E0.31B5C5A9%40cornell.edu&rnum=2
Gambit
 

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

(Oren) wrote:
Quote
when I first reverse the content of Memo via this:
void __fastcall TfrmStringDetails::cmdText_ReverseClick(TObject *Sender)
{
AnsiString temp = Memo1->Text;
strrev(temp.c_str());
Application->ProcessMessages();
Memo1->Text = temp;
}
That reverses all.
And your lineends changed from "\r\n" to "\n\r" now.
So put them back in the right order.
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString temp = Memo1->Text;
strrev(temp.c_str());
temp = StringReplace (
temp, "\n\r", "\r\n", TReplaceFlags() << rfReplaceAll );
Memo1->Text = temp;
}
void __fastcall TfrmStringDetails::cmdLine_NumbersClick(TObject *Sender)
{
for(int i = 0; i < Memo1->Lines->Count; i++)
{
Memo1->Lines->Strings[i] =
String(i + 1)
+ ".) " +
Memo1->Lines->Strings[i];
}
}
Hans.
 

Re:Strange bug...reversing a stringlist and than inserting a linenumber...(Repost = Wrong Group)

thank you very very much :-)
Oren