Logan B. Lehman
Logan B. Lehman

Reputation: 5007

VB.NET Textbox KeyDown Event Not Firing

So,

I have a textbox on a tabcontrol page, and when someone tabs out of the textbox, it is suppose to move to the next tab. This was working great before I switched the form from a UserControl to an actual Form. This change did not change actual code.

So now, I have tried everything. I have the textbox to set to AcceptTab = True, and I have KeyPreview = False (because if the form grabs the event before the textbox does, it would mess things up I am assuming).

Here is my code for the textbox:

 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

Any ideas guys?

Upvotes: 2

Views: 3791

Answers (1)

LarsTech
LarsTech

Reputation: 81675

The quick fix:

txtMsgDTG.Multiline = True

The other fix where you can keep Multiline = False is to subscribe to the TextBox's PreviewKeyDown event:

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

Upvotes: 2

Related Questions