简体   繁体   English

无法在 Windows Forms 文本框上设置焦点

[英]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).当标签页第一次出现时,我似乎无法将输入焦点放在文本框上(我使用的是 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.在标签页的 Enter 事件处理程序中,我调用了文本框的 Focus 方法,但它返回 False 并且什么都不做,没有错误消息。 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.我想将焦点放在 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?如果设置焦点是最好的方法,为什么 textbox.Focus() 方法会失败?

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.如果是这样,您需要执行Me.Show()来实际创建屏幕控件,然后才能设置焦点。 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.如果您不执行Me.Show() ,则在加载事件完成之前不会显示表单。

For the tab control, handle the _SelectedIndexChanged event:对于选项卡控件,处理_SelectedIndexChanged事件:

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.OnShown事件中执行control.Focus() You don't need any of the DoEvents logic which didn't work for me anyway.你不需要任何对我不起作用的 DoEvents 逻辑。

You Should Use Selected Event of TabControl您应该使用TabControlSelected事件

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 .正如我检查了TabControl.SelectedTabPage.Enter事件可以设置 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. (1)设置控件的TabIndex为0。

(2) In your code that handles the tabpage event, do the following: (2) 在处理 tabpage 事件的代码中,执行以下操作:

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:如果 SendKeys 似乎不是一个有效的语句,请确保在代码文件的顶部有以下导入:

Imports System.Windows.Forms

I found that the TabControl gets the focus when the Selected event completes.我发现当 Selected 事件完成时 TabControl 获得焦点。 To make this work I used the Paint event of the TabPage to set the focus of the desired object.为了完成这项工作,我使用 TabPage 的 Paint 事件来设置所需 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:尝试如下形式的Activated事件:

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.我曾经遇到过同样的问题,但我使用Me.activate() function 解决了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vb.net无法将焦点设置为Windows窗体文本框 - vb.net Can't set focus to windows form textbox 在WPF文本框中设置焦点 - Set focus in WPF textbox Windows窗体:以模式方式打开另一个对话框窗口时,如何在文本框中保持焦点和光标 - Windows Forms: How keep focus and cursor on a textbox when another dialog window is open modally 进入标签页时无法将焦点强制到文本框 - Can't force focus to a textbox when entering a tabpage 限制可以输入Windows窗体文本框的字符数 - Limiting the number of characters you can enter into a Windows Forms textbox 设置无法删除的默认文本框值 - Set default textbox value that can't be removed 在 Windows Forms 中将焦点设置为深度嵌套控件的最可靠方法是什么? - What's the most reliable way to set focus to a deeply nested control in Windows Forms? 仅通过焦点将消息发送到任何Windows文本框 - Sending a message to any windows textbox just by focus 将焦点设置到 WebBroswer 控件中的 HTML 文本框或按钮 - Set the focus to a HTML Textbox or Button in a WebBroswer control 如何设置Winform文本框字段焦点,以便用户可以通过单击“选项卡”按钮来浏览它们? - How to Set Up Winform Textbox Field Focus so a User Can Go Through Them by Clicking Tab Button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM