简体   繁体   中英

Can't set focus on a Windows Forms textbox

I can't seem to get input focus on a textbox when a tab page first comes up (I'm using Windows Forms, VB.NET 3.5).

I have a textbox on a panel on a tab page, and I want the focus to be on the textbox when the tab page comes up. I want the user to be able to start typing immediately in the focused textbox without having to click on the textbox. I have tab stops set in the order I want and the textbox is the first tab stop. The tab stops work except that when the tab page comes up the focus is not on the textbox, ie the one that's first in the tab order.

In the Enter event handler of the tab page I call the Focus method of the text box, but it returns False and does nothing, no error messages. I know I can access the text box because at the same point in the code I can set the text of the text box.

If it matters, the layout of the tab page is a little complicated:

frmFoo/TabControl1/TabPageX/Panel1/Panel2/TextBox1

I want to set the focus on TextBox1.

  1. What's the best way to get the focus on the desired textbox?
  2. If setting focus is the best way, why is the textbox.Focus() method failing?

I would assume you are attempting to set focus in the form load event handler? If so, you need to do a Me.Show() to actually create the onscreen controls before focus can be set. Something along the lines of:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Me.Show()
    Application.DoEvents()
    TextBox1.Focus()
End Sub

If you don't do the Me.Show() , the form is NOT displayed until the load event is complete.

For the tab control, handle the _SelectedIndexChanged event:

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
  Handles TabControl1.SelectedIndexChanged

    If TabControl1.SelectedTab.Name = "TabPage1" Then
        TextBox2.Focus()
    End If
    If TabControl1.SelectedTab.Name = "TabPage2" Then
        TextBox4.Focus()
    End If

You will still want to set the initial focus in the load event as shown above if the first field selected is to be the textbox on the tab control.

Try either:

Me.ActiveControl = TextBox1

or

TextBox1.Select()

Do the control.Focus() in the OnShown event. You don't need any of the DoEvents logic which didn't work for me anyway.

You Should Use Selected Event of TabControl

Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
    If e.TabPage.Name = "TabPage1" Then
        TextBox1.Select()
    End If
End Sub

As I have Checked in Both TabControl.Selected and TabPage.Enter Event can set Select TextBox . I think there is some other elements stealing focus. please varify

Any of the solutions I found online don't solve the problem when the control is on a tab page.

However, this works:

(1) set the TabIndex of the control to 0.

(2) In your code that handles the tabpage event, do the following:

SendKeys.Send("{TAB}")

If SendKeys doesn't seem to be a valid statment, make sure you have the following import at the top of your code file:

Imports System.Windows.Forms

I found that the TabControl gets the focus when the Selected event completes. To make this work I used the Paint event of the TabPage to set the focus of the desired object.

Private Sub TabChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tab1.Paint, Tab2.Paint, Tab3.Paint

    Select Case sender.Name
        Case "Tab1"
            Textbox1.Focus()
        Case "Tab2"
            T3extbox2.Focus()
        Case "Tab3"
            Textbox3.Focus()
    End Select

End Sub

Try the Activated event of the form like this:

Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    'SendKeys.Send("{TAB}") this line works too
    TextBox1.Focus()
End Sub

That is guaranteed to work.

I once had the same problem but i solved it using the Me.activate() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM