关于vb.net:将焦点设置为文本框控件

Setting focus to a textbox control

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

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

编辑
换句话说,我希望能够在表单出现后立即直接在文本框中输入内容,而不必手动制表符或手动选择它。


因为要在加载表单时进行设置,所以必须首先.Show()表单,然后才能调用.Focus()方法。 在显示表单之前,表单无法将焦点放在Load事件上

1
2
3
4
5
Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

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


我认为您正在寻找的是:

1
textBox1.Select();

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

从http://msdn.microsoft.com/zh-cn/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 Select method
or the ActiveControl property for child controls, or the Activate
method for forms.


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

使用焦点方法

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


非常简单 :

对于选项卡控件,您需要处理_SelectedIndexChanged事件:

1
2
3
4
5
6
7
8
9
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

要设置焦点,

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

将TabIndex设置为

1
Me.TextBox1.TabIndex = 0


创建一个文本框:

1
2
3
 <TextBox Name="tb">
 ..hello..
</TextBox>

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

1
tb.focus()