[英]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 上。
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
您应该使用
TabControl
的Selected
事件
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.Selected
和TabPage.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.