TextBox Enter键 - 主面板中的子面板

时间:2012-07-30 17:43:55

标签: vb.net

我遇到了这种情况的问题(参见标题)。我在一个大面板中有6个子面板。我创建了一个继承自主Textbox的TextBox类。我正在尝试使用KeyPressed事件处理程序来处理Enter键。当用户按下Enter键时,它从子面板内的一个TextBox移动到下一个子面板。到目前为止,我已经让Enter键事件处理程序适用于焦点所在的面板,而不会跳转到下一个面板。

下面是我用来控制动作的子程序。 问题是我无法从一个子面板跳到另一个子面板。任何帮助将不胜感激!

Protected Shared Sub NextControl(ByVal tControl As Control, ByVal Direction As Boolean)

    Dim pControl As Control = tControl.TopLevelControl
    tControl = pControl.GetNextControl(tControl, Direction)

    If Direction = False Then
        Dim tParent As Control
        While TypeOf tControl Is UserControl
            tParent = tControl.Parent
            tControl = pControl.GetNextControl(tControl, Direction)
            If tControl.Parent Is tParent Then
                Exit While
            End If
        End While
    End If

    If tBox_P00.ControlNesting > 0 Then
        'Dim i As Integer
        pControl = tControl.Parent
        For i As Integer = 0 To tBox_P00.ControlNesting - 2
            pControl = pControl.Parent
        Next
    End If

    If Not tControl Is Nothing Then
        Do Until (tControl.TabStop = True) AndAlso (tControl.Enabled = True) AndAlso (tControl.Visible = True) AndAlso (TypeOf tControl Is Tbx00)

            tControl = pControl.GetNextControl(tControl, Direction)

            'Last in the Panel
            If tControl Is Nothing Then

                tBox_P00.Select(0, tBox_P00.TextLength)
                Beep()
                Exit Sub

            End If
        Loop
        tControl.Focus()
    Else
        tBox_P00.Select(0, tBox_P00.TextLength)
        Beep()
    End If

    Exit Sub
End Sub

1 个答案:

答案 0 :(得分:0)

听起来好像让事情变得复杂。正如HansPassant所提到的,您可以使用GetNextControl为您完成工作:

当按下enter键时,此代码会将焦点移动到表单上的下一个文本框(based on tab index order):

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        Dim ctl As Control = CType(sender, Control)
        Do
            ctl = Me.GetNextControl(ctl, True)
        Loop Until TypeOf ctl Is TextBox
        ctl.Focus()
    End If
End Sub

然后,您可以展开它以处理所有文本框KeyDown事件。