473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3806
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tony Pirkle | last post by:
I'm on a team converting a large VB6 enterprise solution into a VB.Net solution. We've started small. In other words, we're taking MS' advice and beginning with small pieces. I've created a...
6
by: Manish | last post by:
In my application there is need for only upper case chars.. Currently I am making entry to upper case when user leaves focus of the text control. I want to do some modification here...When user...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
2
by: Adam J. Schaff | last post by:
I have recently noticed an unwanted behavior that I do not know how to get rid of. To Recreate Problem: Windows Forms App with 2 forms. Form 1 has nothing on it and this code underneath: ...
5
by: Rico | last post by:
Hello, Is there any way I can tell which key was pressed in VB Code using the OnKeyPress event? I'd like to know if the user is pressing the up, down, left or right arrow. Thanks!
4
by: Brenton Garman | last post by:
Hello. I have managed to consume the key 9 when it is pressed, by using the following code: Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As...
2
by: David McDivitt | last post by:
Another fellow here must modify an application. On the web page, pressing the enter key causes the form to submit. What users want is to remove enter key functionality from within a textarea and...
3
by: Gunnar Syren | last post by:
I'm trying to implement a macro feature in my application by recording and playing back keystrokes. At first I thought it would be enough to catch KeyDown in my main form, but I soon realized that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.