Explanation:
VCL components (e.g. TPanel ) usually have an internal method called Invalidate() that is called when the property changes (e.g. Caption ) and that the change requires repainting part of the control (e.g. draw new text for the label).
This method sets the flag inside the window control, but does not call the repaint method. The reason for this is to avoid calling the Repaint() method several times if many properties are changed immediately (sequentially, in a short time).
The Repaint() method is actually called when the component receives a message for redrawing through the main message loop (processed from the main application thread - GUI thread).
The way you start playing the media player is blocked because you set the Wait property to True , which forces the player to block the calling stream (again the main stream) until the file is played.
This prevents the main thread from processing the message queue and initiating a redraw.
Quick fix:
A quick fix is the one suggested by becsystems, or this one:
panel1.Caption := 'This is a sentence'; Application.ProcessMessages();
Calling ProcessMessages() will give the main thread the ability to process the message queue and perform an update immediately before the file starts playing.
This is a quick fix, as the main stream will still be blocked after the start of playback, which will prevent redrawing other parts of the window (for example, try moving the window or minimizing it and maximizing it during the game).
The code suggested by becsystems is similar, but instead of processing the message queue, it simply forces the control to redraw.
Correct fix:
To fix the problem correctly, you should not use the Wait property and instead handle the OnNotify event of the media player.
Here is an example adapted from the Swiss Delphi Center (not tested since I did not install Delphi at the moment):
procedure TForm1.BitBtn1Click(Sender: TObject); begin panel1.Caption := 'This is a sentence'; with MediaPlayer1 do begin Notify := True; OnNotify := NotifyProc; Filename := 'f:\untitled.wma'; Open; Play; end; end; procedure TForm1.NotifyProc(Sender: TObject); begin with Sender as TMediaPlayer do begin case Mode of mpStopped: {do something here}; end; // Set to true to enable next-time notification Notify := True; end; end;
Side notes:
The following is a brief description of the VCL message loop (part of the Delphi Developer's Guide):
Message System Anatomy: VCL
Also not related to the problem, but see the Delphi Coding Style Guide . It is just nice when the submitted code is formatted.