whytheq whytheq - 2 months ago 6
Vb.net Question

Setting focus to a textbox control

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.

If I want to achieve the same result at run-time, using code, how should I proceed?

Are there alternatives to using tabOrder?

I assume any run-time code will be in the form's constructor or its onload event handler?




EDIT

In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.

Answer

Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form

Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.Show()
    TextBox1.Focus()
End Sub