Hi There,
I am developing VB.NET 2003 window application having problem with the FORM KEYDOWN event.

Problem with cancelling the keydown event when the data check function return FALSE due to duplicate Debtor ID.
How to cancel the KeyDown

[B]Private Sub FrmDebtor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

	If e.KeyCode = Keys.Tab Or e.KeyCode = keys.Enter Then
	        If Me.TabIndex = 1 Then
	 	If FCheckDuplicate = True Then
			e.KeyCode = Keys.Cancel
		End If
	       End If
	End If	
End Sub[/B]

Recommended Answers

All 2 Replies

The Form's KeyDown event will only fire if the form has focus, if a textbox or button has focus then the KeyDown event will be raised on the control, not the form.
If you are checking the tabindex, i'm assuming you only want to process the KeyDown if the user is typing in a certain control.
If so, why not just handle the controls KeyDown event? You can use e.SuppressKeyPress = true to prevent the key from being passed to the underlying control.

Hi Ryshad
Thank you for the suggestion. I will try it out and get back to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.