Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 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..

VB
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

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

e.KeyCode = Keys.Return
 
Share this answer
 
v2
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.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900