Previous | Index | Next 

[PRB] Setting the Visible=False for a Frame may cause spurious GotFocus, LostFocus, and Validate events

Here is a minor behavioral difference between VB6 and .NET. Say that you have a Frame control with a button on it that, when clicked, hides the Frame itself. Under VB6 this operation correctly moves the focus to the control “after” the Frame, according to the TabIndex order.

Under .NET, however, this operation moves the input focus to the next control inside the frame just before the entire frame becomes hidden. This additional input shift causes spurious GotFocus, LostFocus, and Validate events for such a control. Depending on the code in these event handlers, the migrated application might behave incorrectly or generate errors.

Worse, when the GotFocus event fires, the Frame’s Visible property uncorrectly returns the value True, even if the GotFocus event fired because the frame has become hidden.

Fortunately, this situation is quite rare and is unlikely to occur in most migrated applications, if your code misbehave for this reason, the recommended solution is that you set a Boolean variable inside the button’s Click event, and test this variable in the event handlers, as in this short example:

         Dim frameIsHidden As Boolean
         Private Sub cmdHide_Click()
             ' let event handlers know that the frame is hidden
             frameIsHidden  = True
             ' now  you can actually hide it
             Frame1.Visible  = False
         End Sub
  
         Private Sub txtField_GotFocus()
             ' ignore this event if the frame is hidden
             If  frameIsHidden Then Exit Sub
             …
         End Sub

 

Previous | Index | Next