Microsoft KB Archive/112675

= PRB: How to Prevent Flicker in the Repaint of a Label =

Article ID: 112675

Article Last Modified on 10/28/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q112675



SYMPTOMS
When a label control fires a change event, the Caption tends to flicker as it is repainted within the control -- more so when the font size is large.



WORKAROUND
The flicker can be avoided if a picture control (with its AutoRedraw property set to true) is used instead of the label control. However, note that a picture control uses considerably more resources than a label. If you're going to replace a lot of labels, replace them all with one picture control (and multiple print statements) rather than with multiple picture controls. Otherwise, you'll run out of system resources.

The following example demonstrates the use of the picture control to prevent the flicker.

 Start a new project in Visual Basic, Form1 is created by default. Add a picture box (Picture1) to the form, and set its autoredraw property to True. Add a timer control (Timer1) to the form and set its interval property to 100.  Add the following code to the Timer1_Timer event: Sub Timer1_Timer ' Reset the picture in Picture1: Picture1.Cls ' Specify the top and left coordinates for the text: Picture1.CurrentX = 100 Picture1.CurrentY = 100 ' Enter the text (in this case the current time): Picture1.Print = Format$(Now, "h:mm:ss AM/PM") End Sub  Run the program.

An Alternative to the Picture Control
Flicker in a label can be reduced considerably by only updating it when absolutely necessary. For example, update it at the end by using code similar to this: Sub Timer1_Timer Dim TimeStr As String TimeStr = Format$(Now, "h:mm:ss AM/PM") If Label1.Caption <> TimeStr Then Label1.Caption = TimeStr End Sub Or by using code similar to this slightly more efficient code: Sub Timer1_Timer Static LastSecond As Integer If Seconds(Now) <> LastSecond Then LastSecond = Seconds(Now) Label1.Caption = Format$(Now, "h:mm:ss AM/PM") End If  End Sub



Steps to Reproduce Behavior
 Start a new project in Visual Basic. Form1 is created by default.</li> Add a label control (Label1) to Form1.</li> Add a timer control (Timer1) to Form1.</li>  Add the following code to the Timer1_Timer event: Sub Timer1_Timer Label1.Caption = Format$(Now, "h:mm:ss AM/PM") End Sub </li> Run the program.</li></ol>

You will notice a flicker of the caption of the label control. If you have trouble seeing the flicker, try increasing the fontsize.

Additional query words: 3.00

Keywords: kbprb KB112675

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.