VB Migration Partner

KNOWLEDGE BASE - Forms and controls


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 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers