首页 文章

在两个面板之间设置制表位

提问于
浏览
2

我有两个面板,每个面板有几个文本框 . 我想要的是看似非常简单的东西:用户在此面板中输入文本框,然后按Tab键跳转到其他面板中的“链接”文本框 .

但是,vb拒绝跳转到其他面板,除非它完成了一个面板内的所有文本框,无论它是什么TapStop .

我试图从文本框中捕获tab键,并将焦点发送到链接的一个但没有成功:按Tab键甚至不触发KeyDown和KeyPress事件 .

我试图首先将TabStop设置为面板,但这也失败了 .

所以,问题仍然存在..如何设置tabstop ..或任何类似的方法,到两个面板之间的文本框,以便当用户在一个面板中按Tab键时,它将切换到另一个面板?

我需要Tab键,而不是任何其他键 .

1 回答

  • 2

    您必须将表单上每个控件的 TabStop 属性设置为 False ,然后自己处理Tab键,您可以这样做:

    Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox6.PreviewKeyDown,
                                                                                                 TextBox5.PreviewKeyDown,
                                                                                                 TextBox4.PreviewKeyDown,
                                                                                                 TextBox3.PreviewKeyDown,
                                                                                                 TextBox2.PreviewKeyDown,
                                                                                                 TextBox1.PreviewKeyDown
        If e.KeyCode = Keys.Tab Then
            Dim controls As Control() = {TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2}
            Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
            Dim nextControl = controls(currentControlIndex + 1)
    
            nextControl.Select()
        End If
    End Sub
    

    您希望能够从Tab控制的每个控件必须位于 Handles 子句中,并且您希望能够Tab键到或来自的每个控件必须在数组中,并且按照您要Tab键的顺序 . 您还应该再次重复数组末尾的第一个控件,以便从最后回到开头 .

    另请注意,默认情况下不会选择任何控件,如果它们都没有停止,在这种情况下,您必须手动 Select 您希望在窗体的 Shown 事件处理程序中具有焦点的控件 .

    编辑:这是一个更完整的例子:

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        'Manually focus the first control after the form is displayed.
        Button1.Select()
    End Sub
    
    'Include all the controls that you want to behave as Tab stops in the Handles clause.
    'The order is unimportant but ordering them you will Tab to them is not a bad idea.
    Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown,
                                                                                                 TextBox1.PreviewKeyDown,
                                                                                                 TextBox4.PreviewKeyDown,
                                                                                                 TextBox2.PreviewKeyDown,
                                                                                                 TextBox5.PreviewKeyDown,
                                                                                                 TextBox3.PreviewKeyDown,
                                                                                                 TextBox6.PreviewKeyDown,
                                                                                                 Button2.PreviewKeyDown
        If e.KeyCode = Keys.Tab Then
            'This array must contain all controls to behave as Tab stops in order and the first must be repeated at the end.
            Dim controls As Control() = {Button1, TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2, Button1}
    
            'Find the currently active control in the array.
            Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
    
            'Get the next control in the manual tab order.
            Dim nextControl = controls(currentControlIndex + 1)
    
            'Focus that next control.
            nextControl.Select()
        End If
    End Sub
    

    该代码适用于以下形式,其中TextBox1,TextBox2和TextBox3在Panel1中,TextBox4,TextBox5和TextBox6在Panel2中:

    Manual Tab Form

相关问题