Capture <Tab> and <Return> key in TextChanged Event

  • Thread starter Thread starter mgfranz
  • Start date Start date
M

mgfranz

Guest
Is it possible to catch the Enter or Tab key in the TextChanged Event?

I am entering a numeric only value on a comboBox, but once this value has been validated I want the user to be able to either Tab or Enter out of the CB once validated, is this possible from within the TextChanged? Here's my code;

Private Sub cbCheckAmount_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cbCheckAmount.KeyPress
'Only allowed characters
Dim allowedChars As String = "0123456789."
If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
'If e.KeyChar <> ChrW(Keys.Return) Or e.KeyChar <> ChrW(Keys.Tab) Then

If e.KeyChar <> ControlChars.Back Or e.KeyChar <> ControlChars.Tab Then

If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character, notify clear and return
nonNumberEntered = True 'Set to True to 'swallow' the keypress and prevent the TextChanged event from firing.
MsgBox("Numbers only", MsgBoxStyle.Exclamation)
cbCheckAmount.Text = ""
cbCheckAmount.Focus()
cbDollarAmount.Text = ""
End If
End If
'End If
End If

'If shift key was pressed, it's not a number.
If Control.ModifierKeys = Keys.Shift Then
nonNumberEntered = True
cbCheckAmount.Text = ""
cbCheckAmount.Focus()
End If

End Sub
Private Sub cbCheckAmount_TextChanged(sender As Object, e As EventArgs) Handles cbCheckAmount.TextChanged
'Call the function to create a text line out of the numbers
'Regex to ensure the string contains numbers
Dim t As ComboBox = sender
Dim foo As Decimal

If Decimal.TryParse(cbCheckAmount.Text, foo) Then
'data is good
Dim re As New Text.RegularExpressions.Regex("\d")
If re.IsMatch(cbCheckAmount.Text) Then
If nonNumberEntered = False Then
Dim newNum = cbCheckAmount.Text.Trim
'If there are any leading weird . in the string
newNum = newNum.TrimStart(".")
Dim newStr As String
'Build the array
Dim newDec As String() = newNum.Split(New Char() {"."c})
If newNum.Contains(".") Then
newStr = NumberToText(newDec(0))
lblTotalText.Text = newStr & " Dollars and " & newDec(1) & "/100 "
Else
newStr = NumberToText(newDec(0))
lblTotalText.Text = newStr & " Dollars and 00/100 "
End If
End If

End If

Else
'data is bad
nonNumberEntered = False
cbCheckAmount.Text = ""
cbCheckAmount.Focus()
lblTotalText.Text = ""
End If

End Sub

What happens is that which each valid numeric to cbCheckAmount, the sub NumberToText is called and submits a text value to lblTotalText, the code works fine, but how do I allow the user to either <Tab> or <Cr> out of the cbCheckAmount to cbMemoBox and retain the values in both cbCheckAmount and lblTotalText? I can capture the <Tab> and <Cr> using PreviewKeyDown, but when I catch either a <Tab> or <Cr> the TextChanged sets nonNumberEntered to False and clears the fields.

Private Sub cbCheckAmount_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles cbCheckAmount.PreviewKeyDown
If e.KeyData = Keys.Tab Or e.KeyCode = Keys.Enter Then
e.IsInputKey = True
If e.KeyData = Keys.Tab Then
MsgBox("Tab key pressed")
ElseIf e.KeyCode = Keys.Enter Then
MsgBox("Enter key pressed")
'cbMemoBox.Focus()
End If
End If
End Sub

Also, if I enter a valid number into cbCheckAmount and try to "Click" out of it into another control, cbCheckAmount and lblTotalText are cleared and focus is set to cbCheckAmount as if nonNumberEntered is set to False. So, how can I allow the <Tab>, <Cr> and be able to click out of cbCheckAmount and retain values?

Continue reading...
 
Back
Top