By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

Blocking Unwanted Keystrokes in KeyPress Event

P: n/a
In VB6 you can block invalid keystrokes in the KeyPress event by setting
KeyAscii = 0.

How can you block invalid keystrokes in VB.NET?
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Override the ProcessCmdKey function on the form as below:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case CType(msg.WParam.ToInt32, Keys)
Case Keys.Enter
MsgBox("enter")
Return True
Case Keys.Back
MsgBox("Backspace")
Return True
Case Else
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function

the "Return True" indicates you've processed that key and don't want the
base class (the form) to process it.
This way you can block virtually any type of key stroke (well, atleast those
that are defined in the 'Keys' enum).

hope this helps..
Imran.

"Phil Galey" <pa*****@starcalif.com.nospam> wrote in message
news:Ou*************@TK2MSFTNGP12.phx.gbl...
In VB6 you can block invalid keystrokes in the KeyPress event by setting
KeyAscii = 0.

How can you block invalid keystrokes in VB.NET?

Nov 20 '05 #2

P: n/a
Override the ProcessCmdKey function on the form as below:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case CType(msg.WParam.ToInt32, Keys)
Case Keys.Enter
MsgBox("enter")
Return True
Case Keys.Back
MsgBox("Backspace")
Return True
Case Else
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function

the "Return True" indicates you've processed that key and don't want the
base class (the form) to process it.
This way you can block virtually any type of key stroke (well, atleast those
that are defined in the 'Keys' enum).

hope this helps..
Imran.

"Phil Galey" <pa*****@starcalif.com.nospam> wrote in message
news:Ou*************@TK2MSFTNGP12.phx.gbl...
In VB6 you can block invalid keystrokes in the KeyPress event by setting
KeyAscii = 0.

How can you block invalid keystrokes in VB.NET?


Nov 20 '05 #3

P: n/a
Hi,
Handle the KeyPress event of the text box: Check the
KeyPressEventArgs.KeyChar and if it is something you want to block, set the
KeyPressEventArgs.Handled to True.

"Phil Galey" <pa*****@starcalif.com.nospam> wrote in message
news:Ou*************@TK2MSFTNGP12.phx.gbl...
In VB6 you can block invalid keystrokes in the KeyPress event by setting
KeyAscii = 0.

How can you block invalid keystrokes in VB.NET?

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.