?
Solved

KeyPress event - how to cancel illegal characters?

Posted on 2013-05-20
8
Medium Priority
?
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 2000 total points
ID: 39183245
e.Handled = True
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39183286
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
ID: 39183304
>>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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39183322
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
 
LVL 22

Expert Comment

by:Thomasian
ID: 39183360
>>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
ID: 39183451
Thanks, Thomasian.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39183489
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
ID: 39183543
>> 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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