Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4,VB5,VB6
Expertise: beginner
Apr 20, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Don't use SetFocus on invisible controls

In the Show event of a form you might be tempted to use SetFocus to select which field the end user should begin working with. However, this method raises an error 5 when applied on a control that is currently invisible.

If the logic of your application should give the focus always to the same control, it is much better to achieve this using the TabIndex property at design time. Conversely, if the form should behave differently according to other conditions, you should use a different approach. I usually prefer to set the focus in the Form_Activate event

Sub Form_Activate()
  Static done As Boolean
  If Not done Then
    done = True
  End If
End Sub
Note that we need a Static variable to discern the first activation of the form
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date