Board index » cppbuilder » Percent Label for a ProgressBar...

Percent Label for a ProgressBar...


2004-10-12 01:34:54 AM
cppbuilder0
hi dear builders,
i would like to show a Label near my ProgressBar that shows how
many percent of the work has left since the function began..
so i tried the following:
Percent = frmMainUnit->Progress->Max * (pos / 100);
frmMainUnit->Caption = IntToStr(Percent) + " % finshed..";
but the value i get ist endlesssssssss :-)
what do i do wrong..?
here's the full code:
/************************************************************/
void __fastcall ReplaceStrings(const String &a, const String &b, TRichEdit *re)
{
frmMainUnit->Progress->Position = 0;
frmMainUnit->Progress->Max = re->GetTextLen();
int Percent;
TSearchTypes options;
re->Lines->BeginUpdate();
Screen->Cursor = crHourGlass;
try
{
int pos = re->FindText(a, 0, re->GetTextLen(), options);
while(pos != -1)
{
re->SelStart = pos;
re->SelLength = a.Length();
re->SelText = b;
frmMainUnit->Progress->Position = pos;
frmMainUnit->Progress->Update();
Percent = frmMainUnit->Progress->Max * (frmMainUnit->Progress->Position
/ 100);
frmMainUnit->Caption = IntToStr(Percent) + " % finshed..";
Application->ProcessMessages();
pos += b.Length();
pos = re->FindText(a, pos, re->GetTextLen()-pos, options);
}
}
__finally
{
re->Lines->EndUpdate();
frmMainUnit->Progress->Position = 0;
Screen->Cursor = crDefault;
}
}
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
ReplaceStrings("a", "-----", re);
}
/************************************************************/
thanks for any hints on this..
Oren Halvani
 
 

Re:Percent Label for a ProgressBar...

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
so i tried the following:
<snip>
but the value i get ist endlesssssssss :-)
You are using the RichEdit's character count as your ProgressBar's Max
value, but you are not updating the Max whenever you change the character
count during your replacements.
I woudl suggest changing your code to the following instead:
// since the code is accessing frmMainUnit anyway, you
// should make this code be a method of that class...
void __fastcall TfrmMainUnit::ReplaceStrings(const String &a, const
String &b)
{
Progress->Position = 0;
Progress->Max = 100;
TSearchTypes options;
re->Lines->BeginUpdate();
Screen->Cursor = crHourGlass;
try
{
int pos = re->FindText(a, 0, re->GetTextLen(), options);
while( pos != -1 )
{
re->SelStart = pos;
re->SelLength = a.Length();
re->SelText = b;
Progress->Position = ((pos * 100) / re->GetTextLen());
Progress->Update();
Caption = (AnsiString(Progress->Position) + " % finshed..");
Update();
pos += b.Length();
pos = re->FindText(a, pos, re->GetTextLen()-pos, options);
}
}
__finally
{
re->Lines->EndUpdate();
Progress->Position = 0;
Screen->Cursor = crDefault;
}
}
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
ReplaceStrings("a", "-----");
}
Gambit
 

Re:Percent Label for a ProgressBar...

Quote
Percent = frmMainUnit->Progress->Max * (pos / 100);
Percent = (frmMainUnit->Progress->Position * 100) /
frmMainUnit->Progress->Max
/Palle
 

{smallsort}

Re:Percent Label for a ProgressBar...

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
You are using the RichEdit's character count as your ProgressBar's Max
value, but you are not updating the Max whenever you change the character
count during your replacements.

I woudl suggest changing your code to the following instead:

<snip>
...
Quote

Gambit
Remy, first thanks alot ! i got 2 question for you...
1st.) why is it stops now at 99% whats happend with the last 1% ?
2nd.) i've notice that you often use Component->GetTextLen( )
can you tell me where is the difference between:
RichEdit->Text.Length( ) and RichEdit->GetTextLen( ) ??
is it better to use ->GetTextLen( ) instead...?
Oren
 

Re:Percent Label for a ProgressBar...

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
why is it stops now at 99% whats happend with the last 1% ?
Probably because the counting is based on the position of the searching and
the position never reaches the very end of the text. Your original code
would have had that same symptom, assuming your calculation was accurate to
begin with.
Quote
i've notice that you often use Component->GetTextLen( )
can you tell me where is the difference between:

RichEdit->Text.Length( ) and RichEdit->GetTextLen( ) ??
GetTextLen() is much more efficient. All edit controls know their current
text length at all times. There is no need to allocate memory for a
needless temporary AnsiString, and then to copy the current text into that
memory, every time you want to retreive the length only.
Gambit
 

Re:Percent Label for a ProgressBar...

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote

Probably because the counting is based on the position of the searching and
the position never reaches the very end of the text. Your original code
would have had that same symptom, assuming your calculation was accurate to
begin with.

Gambit
Remy, i've done now this...
Label1->Caption = (AnsiString(Progress->Position + 1) + " % finshed..");
now it shows me the full 100% but of course this is the wrong way..
where can be a calculation error..? any ideas..?
Remy one more question, if i use the Update() it will not repaint
all controls on the form, ONLY the label, button and the progressbar,
anything else in invisible, but if use Application->ProcessMessages()
it repaints all the controls..
now, i remember that you often told me to avoid this because it
slows down the application while windows process the messages
to it...SO, how else can i FORCE the repainting of ALL controls..?
do i need to call Update() for each control..? like:
RichEdit->Update;
Edit->Update;
Bevel->Update;
//etc...
or loop them in as component array..? like:
for (int i=0;i<ComponentCount;i++)
{
if(Component)
{
Component->Update();
}
}
Oren
 

Re:Percent Label for a ProgressBar...

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
now it shows me the full 100% but of course this is
the wrong way.. where can be a calculation error..?
There isn't any "error". The code is doing exactly what it should be
doing - the progress is based on the position of the search, and the search
never reaches the end of the text. Sooner or later, the search position
returns -1 rather than the full length of the text. That is normal.
If you must have 100% show up in your progress bar, then simply set it
explitically once your search is finished:
void __fastcall TfrmMainUnit::ReplaceStrings(const String &a, const
String &b)
{
Progress->Position = 0;
Progress->Max = 100;
//...
while( pos != -1 )
{
//...
}
// ... here...
Progress->Position = 100;
Progress->Update();
Caption = "100 % finshed..";
Update();
//...
}
That doesn't really do much for you, thought, since your cleanup code
immediate after the search is resetting the progress back to 0 anyway,
though.
Quote
if i use the Update() it will not repaint all controls on the form,
ONLY the label, button and the progressbar, anything else in
invisible, but if use Application->ProcessMessages()
it repaints all the controls..
Calling Update() on the form itself forces an immediate repaint of any
pending areas of the form that need painting.
If you want to force a repaint of the entire window, use Refresh() or
Repaint() instead.
Gambit
 

Re:Percent Label for a ProgressBar...

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote

Calling Update() on the form itself forces an immediate repaint of any
pending areas of the form that need painting.

If you want to force a repaint of the entire window, use Refresh() or
Repaint() instead.


Gambit
exactly what i wanted !!
Remy THANKS for the great infos !! :-)
Oren