Previous | Index | Next 

[INFO] Controlling the GotFocus and MouseDown event sequence

A key difference between VB6 and VB.NET controls is in the event sequence that they fire when the end user moves the input focus to a control. In such circumstances, VB6 controls fire the MouseDown event and then the GotFocus event, whereas .NET controls do exactly the opposite: they fire the GotFocus event and then the MouseDown event.

Most often than not, this difference is negligible. However, in some cases the code in the GotFocus handler uses one or more variables that have been set in the MouseDown handler. You can force the support library to fire the MouseDown and GotFocus events in the same order as VB6, by setting the VB6Config.FocusEventSupport static property to True:

        ' put this statement in Sub Main
        VB6Config.FocusEventSupport = True

We recommend that you don’t set the FocusEventSupport property to True unless really necessary, because this setting adds overhead to the standard event dispatching process and might cause other minor annoyances.

Unfortunately, the FocusEventSupport property is of limited use in some cases. Consider the following VB6 code:

        ' select the textbox contents when the control gets the focus
        Private Sub Text1_GotFocus()
            Text1.SelStart = 0
            Text1.SelLength = Len(Text1.Text)
        End Sub

You will notice that this code doesn’t behave exactly as in VB6. More precisely:

  1. if the FocusEventSupport property is False (the default), the GotFocus event fires before the .NET Framework can process the mouse action, regardless of whether you have defined a handler for the MouseDown event; the click nullifies the code in the GotFocus handler, and the control contents doesn’t appear to be selected.
  2. If the FocusEventSupport property is True, then the GotFocus event fires after the .NET Framework has processed the mouse action and the contents in the control appears to be selected. However, the .NET Framework hasn’t processed yet the button release action, and as a result the control contents might appear to be selected only partially, depending on where the mouse button is released.


Previous | Index | Next