469,270 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Blocking Unwanted Keystrokes in KeyPress Event

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
3 3606
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
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
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.

Similar topics

6 posts views Thread by Manish | last post: by
3 posts views Thread by Fia | last post: by
2 posts views Thread by Adam J. Schaff | last post: by
5 posts views Thread by Rico | last post: by
4 posts views Thread by Brenton Garman | last post: by
2 posts views Thread by David McDivitt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.