Setting focus

  • Thread starter Thread starter Jack Russell
  • Start date Start date
J

Jack Russell

I have a form containing a tab control
I want focus set to a particular text box when the form opens.

However doing this in the load form event
If TabControl1.SelectedIndex = 0 Then
If txtFileName.CanFocus Then
txtFileName.Focus()
End If

End If
I get false from canfocus.

I can do it fine in the tabcontrol.selectedindexchanged event so why
can't I do it in load?

Thanks
 
I have a form containing a tab control
I want focus set to a particular text box when the form opens.

However doing this in the load form event
If TabControl1.SelectedIndex = 0 Then
If txtFileName.CanFocus Then
txtFileName.Focus()
End If

End If
I get false from canfocus.

I can do it fine in the tabcontrol.selectedindexchanged event so why
can't I do it in load?

Thanks

Controls have not necessarily completed their initilization in the
form's load event. A control to receive the focus, must be enabled,
visible, and have a handle. You may want to move this code to the forms
activated event - though, you will want to put a flag to make sure this
only happens the first time the activated event fires.

You might also make this happen if you do something like:

Me.Visible = True
Me.Refresh
If txtFileName.CanFocus Then
txtFileName.Focus ()
End If

In your load event. This will force the form to become visible and
redraw it self....
 
Tom said:
Controls have not necessarily completed their initilization in the
form's load event. A control to receive the focus, must be enabled,
visible, and have a handle. You may want to move this code to the forms
activated event - though, you will want to put a flag to make sure this
only happens the first time the activated event fires.

You might also make this happen if you do something like:

Me.Visible = True
Me.Refresh
If txtFileName.CanFocus Then
txtFileName.Focus ()
End If

In your load event. This will force the form to become visible and
redraw it self....
Tom,

Thanks,

Had tried it in the activated event to no avail but your second
suggestion worked thanks

Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top