Previous | Index | Next 

[PRB] PictureBox controls can be cleared unexpectedly or receive spurious Paint events

VB6 and VB.NET forms greatly differ in how form refreshes are handled. In VB6, assigning a form property never causes a Paint event or a refresh of its child controls. Conversely, in migrated VB.NET applications assigning a form property – e.g. BackColor or even Text – causes a WM_PAINT and WM_ERASEBKGND messages to be sent to all child controls. How controls react to such messages depends on the specific control.

For most controls – including textboxes, buttons, and listboxes – the extra messages are simply ignored. In some cases – most notable DirListBox and FileListBox – the extra message causes a refresh of the control, so you might notice more flickering than usual.

In the case of PictureBox and UserControl controls whose AutoRedraw property is False, however, the extra WM_PAINT message can cause a couple of undesired effects. First, the control receives a spurious Paint event: if the code inside the Paint event handler clears and redraws the control’s graphic contents you will experience flickering.

Also, if the control’s AutoRedraw’s property is False then the extra WM_ERASEBKGND message causes the control’s background to be cleared. If the PictureBox or UserControl’s content has been created by means of graphic statements – e.g. Line, Circle, or PaintPicture – the content disappears.

A simple way to work around this issue is revising the order of statements and ensuring that statements that affect the form’s properties – and therefore indirectly cause a refresh in child controls – precede the statements that produce graphic output in the PictureBox or UserControl controls. For example, let’s say that you have this VB6 code:

        Me.Picture2.PaintPicture Picture1.Picture, 10, 20
        Me.Caption = "New image"

If Picture2.AutoRedraw is False, then the assignment to the form’s Caption property indirectly clears the contents of the Picture2 control, thus nullifying the effect of the PaintPicture method. You can avoid the problem by simply reversing the two statements:

        Me.Caption = "New image"
        Me.Picture2.PaintPicture Picture1.Picture, 10, 20

If changing the statement order isn’t practical, the only other remedy is setting the PictureBox’s AutoRedraw property to True.

 

Previous | Index | Next