简体   繁体   English

进入标签页时无法将焦点强制到文本框

[英]Can't force focus to a textbox when entering a tabpage

I have a tabcontrol. 我有一个tabcontrol。 On load I am able to force focus to a textbox as required. 在加载时,我能够根据需要将焦点强制到文本框。 If the user opens the second tab I cannot get focus back automatically to the required textbox. 如果用户打开第二个选项卡,我将无法自动将焦点返回到所需的文本框。

I have tried: tabpage1.entering tabpage1.validated tabpage1.gotfocus tabcontrol1.selectedindexchanged 我试过了:tabpage1.entertabpage1.validated tabpage1.gotfocus tabcontrol1.selectedindexchanged

What is strange to me is if the user goes to second tab page then back, the gotfocus event apparently does not fire for the first tab page?? 对我来说奇怪的是,如果用户转到第二个标签页然后返回,则对第一个标签页显然没有触发getfocus事件?

No luck. 没运气。 Any ideas would be great 任何想法都很棒

Here's an example of code I have tried.. 这是我尝试过的代码示例。

 Private Sub TabPage1_Click(sender As Object, e As System.EventArgs) Handles TabPage1.Click

        TB_Input.Clear()
        TB_Input.Focus()

    End Sub

Use the TabControl.SelectedIndexChanged event, and try something like this... 使用TabControl.SelectedIndexChanged事件,然后尝试类似的事情...

Private Sub TabControl1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
    If (TabControl1.SelectedIndex > -1) Then
        If TabControl1.SelectedIndex = 0 Then ' First tab
            TextBox2.Focus()
        ElseIf TabControl1.SelectedIndex = 1 Then ' Second tab
            TextBox3.Focus()
        End If
    End If
End Sub

UPDATE UPDATE

The event fires for me. 该事件为我着火。 In your Visual Studio, make sure, in the Designer View, that the TabContol's SelectedIndexChange event has been assigned. 在Visual Studio中,确保在Designer视图中已分配TabContol的SelectedIndexChange事件。

在此处输入图片说明

OK ! 好 ! we assume you have TabControl1 我们假设您有TabControl1
with TabItem1 and TabControlPanel1 where you drag a TextBox1 与TabItem1和TabControlPanel1拖动文本框1
and TabItem2 and TabControlPanel2 where you drag a TextBox2 和TabItem2和TabControlPanel2,在其中拖动TextBox2

Paste this in your .vb file 将此粘贴到您的.vb文件中

  Private Sub TabItem1_Click(sender As Object, e As EventArgs) Handles TabItem1.Click
    TextBox1.Focus()
End Sub
Private Sub TabItem3_Click(sender As Object, e As EventArgs) Handles TabItem2.Click
    TextBox2.Focus()
End Sub

Try to look under textbox properties... see if the TabIndex number is the first of all other tab indexes. 尝试在文本框属性下查看...查看TabIndex号是否是所有其他标签索引中的第一个。

Mostly, .Focus() only works when called which excludes form_load. 通常, .Focus()仅在调用时才起作用,但不包括form_load。 I've tried myself adding .Focus() upon form load but didn't work and checked the Tab Index.. The first input box has, for example, #43 and the second input box has #42... given the numbers, the cursor will surely focus at the second input box. 我试过自己在窗体加载时添加.Focus(),但不起作用,并检查了Tab索引。.第一个输入框具有例如#43,第二个输入框具有#42 ...给定数字,光标肯定会聚焦在第二个输入框上。 If the other control, like image or grid, has lower number than input boxes then surely, there will be no focused box. 如果其他控件(例如图像或网格)的数量少于输入框的数量,那么肯定会没有焦点框。

The tab index is auto-generated. 标签索引是自动生成的。 The number depends on who was created first than who. 数量取决于谁先创造。 This works in mine, if this works in your problem too... please let me know. 这在我的作品中有效,如果在您的问题中也适用...请让我知道。 :) :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM