Solved

KeyPress event - how to cancel illegal characters?

Posted on 2013-05-20
8
437 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
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
Industry Leaders: 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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