Solved

KeyPress event - how to cancel illegal characters?

Posted on 2013-05-20
8
441 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 500 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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