Can I do this - Control Focus despite Tab Order ?

J

JB

Using the tab order in VB on a form works fine but to me it seems
tedious (at times) to have to set or be concerned with the tab order.
I can't seem to get a control to have focus on the load form
event...am I doing something wrong or do I just need to control this
thru tab order?

TIA

JB
 
I

Imran Koradia

The form is shown only after the last line in the load event is executed.
During the load event, the form is invisible and so are the controls on it.
Focus can only be set on controls that are visible and enabled. So trying to
set focus to a control in the load event wont work. Instead, use the
Activated event of the form to set focus. However, since Activated is fired
everytime the form is activated, you'll have to make sure that you set focus
only the first time it is activated which is after the load event. Something
like this should work for you:

Private bFirstTimeActivated as boolean

Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
If Not bFirstTimeActivated Then
MyControl.Focus()
bFirstTimeActivated = True
End If
End Sub


hope this helps..
Imran.
 
J

JB

Thanks for the explanation...makes sense. I'll give your method a
try.

One other thing that I discovered right after I posted my original
message was the "Tab Order" found under the View menu when in Design
mode. This feature takes the tediousiness out of setting tab order.

JB
 

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

Top