VB Migration Partner

KNOWLEDGE BASE - Forms and controls

Previous | Index | Next 

[PRB] The MsgBox, InputBox, and common dialogs cause spurious LostFocus and GotFocus events

Under VB6, displaying a modal dialog – such as those displayed by the MsgBox and InputBox methods, or any common dialog – doesn’t cause any event for the control that has the input focus. Conversely, under .NET these actions cause a LostFocus event when the dialog becomes visible and a GotFocus event when the dialog is closed and the active control regains the input focus.

This behavioral difference can cause a problem in the migrated .NET project if you use the LostFocus and GotFocus events to perform specific actions. Two solutions exists for this problem, however both of them require manual edit of the migrated code.

VB Migration Partner’s support library exposes the IsMsgBoxActive flag that is True while one of these modal dialogs are visible, therefore it’s easy to immediately exit a “spurious” LostFocus that wouldn’t be raised under VB6:

        Private Sub Text1_LostFocus()
            ' exit if this event is caused by a modal dialog
            If IsMsgBoxActive Then Exit Sub
        End Sub

The VisualBasic6_Support.bas file contains a Boolean variable named IsMsgBoxActive, thus you can include the file to the current project and use the previous code even in the original VB6 code. Under VB6 the variable is always false, therefore the previous code works correctly in both VB versions.

Unfortunately, you cannot easily detect spurious GotFocus using this technique, because the dialog is already closed when the .NET control takes the input focus. In this case you are forced to adopt the second solution, which consists of using the Enter event instead of the GotFocus event. (Microsoft warmly recommends that you always use the Enter and Leave events instead of GotFocus and LostFocus events.)

The easiest way to automatically convert all your GotFocus event handlers into Enter handlers is by means of a PostProcess:

        '## PostProcess "\(\) Handles (?<ctrl>\w+)\.GotFocus", _
                 "(ByVal sender As Object, ByVal e As EventArgs) Handles ${ctrl}.Enter"
Previous | Index | Next 

Follow Francesco Balena on VB6 migration’s group on


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.


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