Bsheikh

Hi experts,

i need to capture keydown event on TAB OR ENTER KEY. now i knew that hitting TAB will not raise key down or keypress event . please help me to play around with that.

i need to raise keydownevent/keypress event with TAB and ENTER key.

If e.KeyCode = Keys.Tab Or e.KeyCode = Keys.Enter Then

If Not IsNumeric(txtupc.Text) AndAlso txtupc.Text.Length <> 12 Then

MsgBox("Invalid Entry")

Return

end if

Exit Sub

Regards,

Bilal



Re: Windows Forms General CAPTURING enter AND tab KEY EITHER

rwerner

Just add this special event handler to your code (it's in C# but I'm sure you can easily convert the syntax for VB) :


// This is a special event handler to trap the Tab key.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
{
// Do your stuff, comment out the next line if the Tab should still be handled
return true;
}

return base.ProcessCmdKey(ref msg, keyData);
}





Re: Windows Forms General CAPTURING enter AND tab KEY EITHER

nobugz

The best way to do what you want to do is to implement the Validating event. For example:

Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Or TextBox1.Text.Length <> 12 Then
MsgBox("Invalid entry")
e.Cancel = True
End If
End If
End Sub

Note that I allowed the user to tab through an empty textbox, just remove the test on the Length property if you don't want to do that. Beware that there are problems with this code if the textbox is hosted on a TabControl page.

To make the Enter key behave like Tab, check this thread...