Board index » cppbuilder » Label Caption Is not shown ?

Label Caption Is not shown ?


2007-04-17 07:43:11 PM
cppbuilder13
Hi,
My example is with Button, Sleep and Label as below:
//--------------------------------------------------------------------------
-
void __fastcall Form::Button1Click(TObject *Sender)
{
Label1->Caption = "aaaa";
Sleep(2000);
Label1->Caption = "bbbb";
Sleep(2000);
Label1->Caption = "cccc";
}
//--------------------------------------------------------------------------
-
I click the button, and then I expected "aaaa" or next to "bbbb" should be
shown before "cccc" on the screen, but my expection was wrong. Do we have
the other button instead of the standard button?
Best regards,
Robert
 
 

Re:Label Caption Is not shown ?

You shouldn't write code like this.
The graphics part of the app is single threaded and while
your button click is being run, windows won't update
displays.
This is because the message to tell windows to update the
button caption is processed by the same thread as the button
click handler.
If you want to do this, you should call
Application->ProcessesMessages() before each sleep() but
it's not the right way to do this as the sleep() is stopping
the application working efficiently.
If you want to do something similar, use a Timer to go
through different states and update the caption.
Sleep()s in button clicks are examples of poor design.
HTH Pete
"Robert" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

My example is with Button, Sleep and Label as below:


I click the button, and then I expected "aaaa" or next to
"bbbb" should be
shown before "cccc" on the screen, but my expection was
wrong. Do we have
the other button instead of the standard button?
 

Re:Label Caption Is not shown ?

I solved by using Application->ProcessMessages() and Timer() instead of
Sleep().
Thank you so much.
Robert
"Pete Fraser" < XXXX@XXXXX.COM >wrote in
message news:4624b4f1$ XXXX@XXXXX.COM ...
Quote
You shouldn't write code like this.
The graphics part of the app is single threaded and while
your button click is being run, windows won't update
displays.
This is because the message to tell windows to update the
button caption is processed by the same thread as the button
click handler.
If you want to do this, you should call
Application->ProcessesMessages() before each sleep() but
it's not the right way to do this as the sleep() is stopping
the application working efficiently.
If you want to do something similar, use a Timer to go
through different states and update the caption.
Sleep()s in button clicks are examples of poor design.

HTH Pete

"Robert" < XXXX@XXXXX.COM >wrote in message
news:4624b2ce$ XXXX@XXXXX.COM ...
>Hi,
>
>My example is with Button, Sleep and Label as below:
>
>
>I click the button, and then I expected "aaaa" or next to
>"bbbb" should be
>shown before "cccc" on the screen, but my expection was
>wrong. Do we have
>the other button instead of the standard button?


 

{smallsort}

Re:Label Caption Is not shown ?

Robert wrote:
Quote
My example is with Button, Sleep and Label as below:
//-----------------------------------------------
void __fastcall Form::Button1Click(TObject *Sender)
{
Label1->Caption = "aaaa";
Update();
Sleep(2000);

Label1->Caption = "bbbb";
Update();
Sleep(2000);

Label1->Caption = "cccc";
}
//------------------------------------------------
I click the button, and then I expected "aaaa" or next to "bbbb"
should be shown before "cccc" on the screen, but my expection was
wrong.
--
Alex
 

Re:Label Caption Is not shown ?

Quote
void __fastcall Form::Button1Click(TObject *Sender)
{

Label1->Caption = "aaaa";
Sleep(2000);

Label1->Caption = "bbbb";
Sleep(2000);

Label1->Caption = "cccc";
}
//--------------------------------------------------------------------------
-


I click the button, and then I expected "aaaa" or next to "bbbb" should be
shown before "cccc" on the screen, but my expection was wrong. Do we have
the other button instead of the standard button?
No problem with Sleep().
If you want "information flash" only and the user must wait it you may
use Sleep() but you need Repaint() too !
Only if you want enable "other events" then you need ProcessMessages() ;)
I use below code well:
void WLabelFlash(TLabel * lblMessage)
{ // Flash now displayed last error message
TColor OldColor = lblMessage->Color;
lblMessage->Color = clYellow;
lblMessage->Repaint();
WSound(wsoundWarn);
Sleep(400);
lblMessage->Color = OldColor;
lblMessage->Repaint();
Sleep(400);
lblMessage->Color = clYellow;
lblMessage->Repaint();
Sleep(400);
lblMessage->Color = OldColor;
lblMessage->Repaint();
}
eMeL
Sorry my terrible English. My native language is C++ ;)