VB.NET文本框KeyDown事件未触发

时间:2012-04-19 14:27:35

标签: vb.net events textbox keydown

所以,

我在tabcontrol页面上有一个文本框,当有人从文本框中选中时,它会移动到下一个选项卡。在将表单从UserControl切换到实际表单之前,这工作得很好。此更改未更改实际代码。

所以现在,我已经尝试了一切。我将文本框设置为AcceptTab = True,并且我有KeyPreview = False(因为如果表单在文本框之前抓取事件,那么我会假设它会搞乱)。

以下是我的文本框代码:

 Private Sub txtMsgDTG_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtMsgDTG.KeyDown

    'check for tabbed out
    If e.KeyCode = Keys.Tab Then
        'If the user tabs into this field after filling out the necessary fields ...
        If txtMsgDTG.Text = String.Empty Then
            'If the user left the field BLANK ...

            'Move to next page:
            TabControl1.TabPages(0).Enabled = True
            TabControl1.TabPages(1).Enabled = True
            TabControl1.TabPages(2).Enabled = False
            TabControl1.TabPages(3).Enabled = False
            TabControl1.TabPages(4).Enabled = False
            TabControl1.SelectedIndex = 1

        Else
            'If the user did NOT leave the field blank ...

            'validate message DTG
            Dim dtgCheck As String
            dtgCheck = ValidateDTG(txtMsgDTG.Text)
            If dtgCheck <> "valid" Then
                MsgBox(dtgCheck)
            Else
                'Move to next page:
                TabControl1.TabPages(0).Enabled = True
                TabControl1.TabPages(1).Enabled = True
                TabControl1.TabPages(2).Enabled = False
                TabControl1.TabPages(3).Enabled = False
                TabControl1.TabPages(4).Enabled = False
                TabControl1.SelectedIndex = 1

            End If

        End If

    End If


End Sub

任何想法的人?

1 个答案:

答案 0 :(得分:2)

快速修复:

txtMsgDTG.Multiline = True

您可以保留Multiline = False的其他修补方法是订阅TextBox的PreviewKeyDown事件:

Private Sub txtMsgDTG_PreviewKeyDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles txtMsgDTG.PreviewKeyDown
  If e.KeyCode = Keys.Tab Then
    e.IsInputKey = True
  End If
End Sub