How do I set focus on a specified textbox at load?

  • Thread starter Thread starter Top Gun
  • Start date Start date
T

Top Gun

I am attempting to set the focus on a calculated textbox during the forms
Load event, however it seems to want to go to the default location instead.

Is there some way I can do this in a way that gurantees it will go where I
want it to go when the form is made visible?
 
I usually use the focus on the Visible event for the form as it is one of
the (if not the) last event to be fired when a form is displayed.
This always worked for me so far
 
Sorry, I tried this but could not get it to work. The only form event that I
could find that appeared to correspond to what you might be referring to is
VisibleChanged, but still the control did not get focus. The code I'm trying
to execute looks something like this:

if Me.txtName.Text = "" then
Me.txtName.Focus
endif
 
You could do this in (at least) two ways:

(1) This way will set focus the first time the application is run.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Me.Show() ' Force the Form to become visible.
Me.TextBox1.Focus()
End Sub

(2) This way will set focus essentially everytime the Form gets focus,
becomes activated.
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Me.TextBox1.Focus()
End Sub

Depending on what type of behavior you are looking for one of these options
might be more favorable over the other.
 
Back
Top