locked
keypress event not working RRS feed

  • Question

  • I've registered a keypress event with the following code:
    [code]
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
              //  if (e.KeyChar == (char)Keys.Return)
                    System.Windows.Forms.MessageBox.Show("test");
            }
    [/code]

    I've tried several different variations, but nothing seems to work.  Is there something I'm mising?

    By the way, in vb.net I could select an event from a drop down box and it would generate the code.  Is this possible in C#?

    Thanks.
    Friday, January 4, 2008 11:49 PM

Answers

  • If you want to do it programaticaly, you need to declare an event handler for your event. You can find a sample on how to handle a keypress event programaticaly here: http://www.demo2s.com/Code/CSharp/Services-Event/Ankeypresseventexample.htm

     

    You can also use the facilities of the Visual Studio IDE and let VS deal with the problem of registering the event. Go to the Designer (shift+f7) and look in the Properties menu. You'll find the KeyPress event (http://img87.imageshack.us/my.php?image=73527315rp9.gif)

    Saturday, January 5, 2008 12:22 AM
  • This topic came up a few weeks ago.  If your form has any controls on it that can take 'focus' (like a button or a textBox) then these controls will consume the key press event and the Form itself will never see it.

     

    Regarding your second question: When you select a control in the designer, look in the Properties window.  There is an icon with a lightning bolt.  Select it and you will see all of the events of that control.  Double click on the event and the handler will be generated and hooked up for you automatically.

    Saturday, January 5, 2008 12:28 AM

All replies

  • If you want to do it programaticaly, you need to declare an event handler for your event. You can find a sample on how to handle a keypress event programaticaly here: http://www.demo2s.com/Code/CSharp/Services-Event/Ankeypresseventexample.htm

     

    You can also use the facilities of the Visual Studio IDE and let VS deal with the problem of registering the event. Go to the Designer (shift+f7) and look in the Properties menu. You'll find the KeyPress event (http://img87.imageshack.us/my.php?image=73527315rp9.gif)

    Saturday, January 5, 2008 12:22 AM
  • This topic came up a few weeks ago.  If your form has any controls on it that can take 'focus' (like a button or a textBox) then these controls will consume the key press event and the Form itself will never see it.

     

    Regarding your second question: When you select a control in the designer, look in the Properties window.  There is an icon with a lightning bolt.  Select it and you will see all of the events of that control.  Double click on the event and the handler will be generated and hooked up for you automatically.

    Saturday, January 5, 2008 12:28 AM
  • Thank you both for providing quality answers to my questions.  I went ahead and used the event view to do the work for me, but I'm now dealing with the problem jrboddie mentioned (regarding losing focus to controls).  In this case, should I just set the keypress event for all of my controls? (There won't be many).
    Saturday, January 5, 2008 6:53 PM
  • Try changing the KeyPreview property of the form to True.  That seems to work for me.

    Saturday, January 5, 2008 8:19 PM