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