Tab键功能使用回车键在VB.Net回车键、功能、Tab、VB

2023-09-04 04:25:03 作者:此号已被盗

我有近20个文本框和5个组合框和一个控制取决于窗体上其他的, 现在我想写的$ C $下的形式,这样的方式,pressing 输入键和标签键应该具有相同的功能。

I have a form with nearly 20 Textbox and 5 Combobox and one control in dependent on the other, Now I want to write the code for the form in such a way that, Pressing Enter Key and Tab Key should have the same functionality.

像pressing 标签键的焦点移动到下一个控制还应当我preSS的输入键进行。 同样,当我preSS的输入键,还有一些过程code写的关键preSS事件,但这个也应该执行,当我preSS的标签键。

Like on pressing Tab Key the focus moves to next control should also be performed when I press the Enter Key. Similarly when I press the Enter Key, there is some process code written in key press event but this should also be performed when I press the Tab Key.

推荐答案

这是我在的WinForms实现它的方法是使用的 SelectNextControl 的方法。

The way that I have accomplished it in Winforms is by using the SelectNextControl Method.

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    Dim tb As TextBox
    tb = CType(sender, TextBox)

    If Char.IsControl(e.KeyChar) Then
        If e.KeyChar.Equals(Chr(Keys.Return)) Then
            Me.SelectNextControl(tb, True, True, False, True)
            e.Handled = True
        End If
    End If
End Sub

如果您正在使用WPF,您可以使用 TraversalRequest

If you are using WPF you can use TraversalRequest

Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
    Dim tb As TextBox
    tb = CType(sender, TextBox)

    If e.Key = Key.Return Then
        tb.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))
    ElseIf e.Key = Key.Tab Then
        Exit Sub
    End If
End Sub

至于拦截标签键来看看这个Stackoverflow问题。