bharathivkmani
bharathivkmani

Reputation: 59

How to Focus a TextBox VB.Net

I have a text box in my form. When clicking the tray icon the form is shown and on form load I have written the code to focus on the text box. But it's not pointing the text box. Let I have 2 forms formB and formC I am calling formC from formB so that I doesn't getting the focus. how to give focus to the second form? I have find the solution to this problem . we have to overwrite the show method. Let's say formA and formB. Then FormB is in focus now.

Private Sub FormB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Timer1.Enabled = True
    Me.Timer1.Interval = 2000
End Sub`

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Enabled = False
    Dim frmquickListPHD1 As New FormC()
    frmquickListPHD1.Show(CallingForm, CallingControl, Me)
End Sub

   Overloads Sub Show(ByVal f1 As Form, ByVal c As Control, ByVal f2 As Form)
    callingform = f1
    MenuForm = f2
    callingcontrol = c
    Show()
End Sub
Private Sub FormC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TextBox1.Focus()
End Sub`

Upvotes: 0

Views: 28443

Answers (3)

senthilkumar2185
senthilkumar2185

Reputation: 2578

Set TabIndex =0

or

after all loaded in form load to set Textbox1.focus()

Upvotes: 0

5uperdan
5uperdan

Reputation: 1501

use the ActiveControl property of your form in the load event of the form:

Me.ActiveControl = Textbox1

Alternatively, if you're showing an instance of FormB from FormA on some event then you could use the following code in FormA:

Dim FormB1 as new FormB
FormB1.show()
FormB1.ActiveControl = FormB1.Textbox1

Don't use focus, from msdn (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx):

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

Upvotes: 1

Matt Wilko
Matt Wilko

Reputation: 27342

The issue may be that the form does not have focus when you open it from the system tray?

Try something like:

frmTest.Show
frmTest.Select

The in your Form_Activated event of frmTest

textbox1.Select

Upvotes: 4

Related Questions