Solved

KeyPress event - how to cancel illegal characters?

Posted on 2013-05-20
8
416 Views
Last Modified: 2013-05-20
Hi Experts,

Here is the code for my KeyPress event.  


    Private Sub txtJobName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtJobName.KeyPress


        'Select Case e.KeyAscii (VB6)
        Select Case e.KeyChar

            'Case 92, 47, 58, 42, 63, 34, 60, 62, 124, 94 (VB6)
            Case "\", "/", ":", "*", "?", Chr(34), "<", ">", "|", "^"

                Beep()
                MsgBox("Sorry, the Job Name cannot include any of the characters \ / : * ? "" < > | or ^")

                'e.KeyAscii = 0 'cancel keystroke (VB6)
                'what is the vb.net code to cancel the illegal character?

        End Select


    End Sub

Open in new window


How can I cancel the illegal characters, so that the text box does not return any illegal characters?

Regards,
Leigh
0
Comment
Question by:LeighWardle
  • 4
  • 3
8 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
Comment Utility
e.Handled = True
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
KeyPress is too late in the event cycle to suppress key presses. Handle the KeyDown event instead. In the handler, set the e.Handled and e.SuppressKeyPress to True.
0
 
LVL 22

Expert Comment

by:Thomasian
Comment Utility
>>KeyPress is too late in the event cycle to suppress key presses.

No, it is not. The "MsgBox" inside the event handler causes the "KeyPress" event to be triggered before the key was suppressed. So in this case, I would go with the "KeyPress" event.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Upon further inspection, this I agree with:

No, it is not.

This I do not:

The "MsgBox" inside the event handler causes the "KeyPress" event to be triggered...
The MsgBox wouldn't cause the KeyPress event to be triggered; the pressing of a key inside of the corresponding TextBox would. I suspect that's not what you meant, but that's the way it reads.

I seem to recall having issue with the KeyPress event some time back. Of course it's been just long enough to escape my memory. I may have been confusing it with the KeyUp event.

There is one caveat in handling the KeyPress event in that you will have to account for casing if you want to exclude certain letters. 'a' and 'A' are passed in as two different pieces of data in the e.KeyChar parameter.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 22

Expert Comment

by:Thomasian
Comment Utility
>>This I do not:

You removed part of the sentence which changes what I was trying to say.

"causes the "KeyPress" event to be triggered before the key was suppressed"

The Msgbox does not trigger the KeyPress event. It simply causes the event to be triggered before the KeyDown event handler could suppress the key (i.e. when the Sub ends).


>>There is one caveat in handling the KeyPress event

I agree it is usually better to suppressing keys in the KeyDown event, which is why I specifically said that in this case, I would use the KeyPress event.
0
 
LVL 1

Author Closing Comment

by:LeighWardle
Comment Utility
Thanks, Thomasian.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
It simply causes the event to be triggered before the KeyDown event handler could suppress the key (i.e. when the Sub ends).
I'm sure I'm still misunderstanding you. A MsgBox is modal. The handler will block until the MsgBox returns. The key will not actually be suppressed until this handler returns to the ProcessKeyEventArgs method of the Control base class (where the value that was assigned to e.Handled will be returned from that method, indicating to the messaging subsystem whether or not to display the character in the box).

The MsgBox has no bearing on whether or not the key will be suppressed or whether or not the event will be "triggered."
0
 
LVL 22

Expert Comment

by:Thomasian
Comment Utility
>> The handler will block until the MsgBox returns.

Yes, but it doesn't seem to prevent the control from triggering the KeyPressed and KeyUp events.

Here's a simple code to show what I mean. Note that the text has already changed even before e.Handled was passed back from the method.
 
  Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Console.WriteLine("KeyDown Start")

        MsgBox("Test")

        Console.WriteLine("KeyDown End")
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Console.WriteLine("KeyPress")
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Console.WriteLine("TextChanged")
    End Sub

Open in new window


Result:
KeyDown Start
KeyPress
TextChanged
KeyDown End

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now