Click here to Skip to main content
14,599,331 members
Rate this:
Please Sign up or sign in to vote.
Hi S Mewara,

I m using vb.net with window application in vs2008.
I have followed this code. it is working fine for Esc key or event is occur.
but when i have used this code for enter key event is not getting..

Private Sub frmScreen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close() 
        ElseIf e.KeyCode = Keys.Enter Then
            'btnPriview_Click(sender, e)
            MessageBox.Show("Fire Enter")
        End If
    End Sub


1. e.KeyCode = Keys.Escape - it is working when Esc key is down.
2. e.KeyCode = Keys.Enter - it is not working when Enter key is down..

How to resolve this problem.......
suggest me

Thanks
mukesh

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 10-Jun-11 21:55pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Jun-11 23:53pm
   
You need to tag it: WPF, Forms, ASP.NET? It looks like Forms, but you really want to put it in the tag.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming that the key does enter the frmScreen_KeyDown sub, check for:

e.KeyCode = Keys.Return
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

It is slightly more complex than you think - it also depends on what controls you have on your form.
If you create a blank form, and handle the Key Down event, you will get all the keys, enter (as e.Return), ESC (as e.Escape), etc.

If you add a button to it, then suddenly the event stops, because the button can take input and thus has the focus - it gets all the Key events instead of the form. So, you need to add the handler to all the controls as well - that's ok, the same handler had be added to them all without any real problem.

But that won't solve your problem either, because some controls (buttons for example) will not only accept input, but will swallow some keys: A standard button for example will swallow the Enter key and produce a Click event. A Cancel button will normally swallow a Escape key, and produce a Click event.

If I were you, I would re-think my keyboard usage - it is easier to work with the standard framework key usage, than the subvert it - and it is easier for your users who may expect keys to do things as they do in other applications.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100