471,896 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

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 3713
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
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.