Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

KeyPress event - how to cancel illegal characters?

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
LeighWardle
Asked:
LeighWardle
  • 4
  • 3
1 Solution
 
ThomasianCommented:
e.Handled = True
0
 
käµfm³d 👽Commented:
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
 
ThomasianCommented:
>>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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
käµfm³d 👽Commented:
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
 
ThomasianCommented:
>>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
 
LeighWardleAuthor Commented:
Thanks, Thomasian.
0
 
käµfm³d 👽Commented:
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
 
ThomasianCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now