vb.net 将焦点设置到文本框控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16294248/
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 16:51:39  来源:igfitidea点击:

Setting focus to a textbox control

vb.netwinforms

提问by whytheq

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.

如果我想在第一次打开表单时将焦点设置在文本框上,那么在设计时,我可以将它的 tabOrder 属性设置为 0,并确保没有其他表单控件的 tabOrder 为 0。

If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?

如果我想在运行时使用代码实现相同的结果,我应该如何进行?
是否有使用 tabOrder 的替代方法?
我假设任何运行时代码都将在表单的构造函数或其 onload 事件处理程序中?



EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.

编辑
换句话说,我希望能够在表单出现时直接在文本框中键入内容,而无需手动使用 Tab 键或手动选择它。

回答by Robert Beaubien

Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form

因为要在表单加载时设置它,所以必须先 .Show() 表单,然后才能调用 .Focus() 方法。在您显示表单之前,表单无法在 Load 事件中获得焦点

Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.Show()
    TextBox1.Select()
End Sub

回答by ispiro

I think what you're looking for is:

我想你要找的是:

textBox1.Select();

in the constructor. (This is in C#. Maybe in VB that would be the same but without the semicolon.)

在构造函数中。(这是在 C# 中。也许在 VB 中也一样,但没有分号。)

From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx:

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 Selectmethod or the ActiveControl property for child controls, or the Activate method for forms.

Focus 是一种低级方法,主要供自定义控件作者使用。相反,应用程序程序员应该对子控件使用Select方法或 ActiveControl 属性,或者对窗体使用 Activate 方法。

回答by dbasnett

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    TextBox1.Select()
End Sub

回答by Bogdan M.

Using Focus method

使用焦点方法

  Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       yourControl.Focus()
    End Sub

回答by Anil

Quite simple :

非常简单 :

For the tab control, you need to 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

回答by Lingasamy Sakthivel

To set focus,

要设置焦点,

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

Set the TabIndex by

设置 TabIndex

Me.TextBox1.TabIndex = 0

回答by Laxmi

create a textbox:

创建一个文本框:

 <TextBox Name="tb">
 ..hello..
</TextBox>

focus() ---> it is used to set input focus to the textbox control

focus() ---> 用于设置文本框控件的输入焦点

tb.focus()