Put focus on a control
February 27, 2009 on 10:40 pm | In Programming | 7 CommentsIf you want to put the focus on a control immediately after the form loads, you need to show the form first.
Example:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Button2.Focus()
End Sub
7 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
Pieter,
I only have an older version of VB. But here on VB6 you can just set that particular control’s TabIndex to 0 and that will set it as having focus when the form loads. Is that still the case or has that changed?
And sure, the form has to be visible for any control to have focus.
Comment by Mosaic1 — March 16, 2009 #
Hi Katie,
Nice of you to drop by.
That still works, but the only way I know how to do that is to edit the forms’ designer.vb and the comments in there state:
‘Do not modify it using the code editor.
Not that that ever stoppped me. 🙂
Comment by metallica — March 16, 2009 #
Thanks Pieter.
Don’t you have a properties Window showing in the ide? If so, you can select the control for that particular form and set its properties at design time.
Also, for tab index, the tab index for a control on a form is determined by the order in which the control is added to the form. So the first control you add has a tab index of 0 and will have focus when the form loads.
Comment by Mosaic1 — March 16, 2009 #
Hi Katie,
I’ve looked at the Properties Windows for a Control and I found a Tabindex property, so I guess you can change those to switch them around in case the following order you added them was not the correct order you wanted for the Tabindex. But I could not find anything in the Properties window of the form that I could use to change the focus.
I’m using Visual Studio 2005 here (at home) and 2008 at work.
Comment by metallica — March 16, 2009 #
Pieter,
A TabIndex of 0 will give that particular control focus. Since you’re adding that to the form load event, you can set the command button’s TabIndex to 0 if you like, as an alternative. You can set the tab order any way you like. this dertermines the order in which the controls will have focus when you press the Tab Button on your keyboard.
Katie
Comment by Mosaic1 — March 17, 2009 #
Got it. Thanks Katie.
Comment by metallica — March 17, 2009 #
You’re welcome. Nice talking to you again!
Comment by Mosaic1 — March 18, 2009 #