vb.net 无法将焦点设置在 Windows 窗体文本框上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6287605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:32:18  来源:igfitidea点击:

Can't set focus on a Windows Forms textbox

vb.netwinforms

提问by RaleighBoy

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 窗体、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, i.e. the one that's first in the tab order.

我在标签页的面板上有一个文本框,当标签页出现时,我希望焦点位于文本框上。我希望用户能够立即在焦点文本框中开始输入,而无需单击文本框。我按照我想要的顺序设置了制表位,文本框是第一个制表位。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.

在标签页的Enter事件处理程序中我调用了文本框的Focus方法,但是它返回False并且什么都不做,没有错误信息。我知道我可以访问文本框,因为在代码的同一点我可以设置文本框的文本。

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?
  1. 将焦点放在所需文本框上的最佳方法是什么?
  2. 如果设置焦点是最好的方法,为什么 textbox.Focus() 方法失败?

回答by Robert Beaubien

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:

我假设您正在尝试在表单加载事件处理程序中设置焦点?如果是这样,您需要Me.Show()在设置焦点之前实际创建屏幕控件。类似的东西:

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 _SelectedIndexChangedevent:

对于选项卡控件,处理_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.

如果选择的第一个字段是选项卡控件上的文本框,您仍然希望在加载事件中设置初始焦点,如上所示。

回答by keyboardP

Try either:

尝试:

Me.ActiveControl = TextBox1

or

或者

TextBox1.Select()

回答by BillJam

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

执行control.Focus()OnShown事件。您不需要任何对我不起作用的 DoEvents 逻辑。

回答by Rajeev

You Should Use SelectedEvent of TabControl

你应该使用Selected事件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.Selectedand TabPage.EnterEvent can set Select TextBox. I think there is some other elements stealing focus. please varify

正如我已经 Checked in BothTabControl.SelectedTabPage.EnterEvent 可以设置 Select TextBox。我认为还有一些其他因素在窃取焦点。请修改

回答by Sketch

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

回答by David

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.

我发现当 Selected 事件完成时 TabControl 获得焦点。为了完成这项工作,我使用了 TabPage 的 Paint 事件来设置所需对象的焦点。

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

回答by djati

Try the Activatedevent 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.

这是保证工作。

回答by antondan

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

我曾经遇到过同样的问题,但我使用该Me.activate()功能解决了它。