Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,163 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




Keypress event not working

 
Reply to this topicStart new topic

Keypress event not working

kurent
27 Dec, 2007 - 08:31 AM
Post #1

New D.I.C Head
*

Joined: 27 Dec, 2007
Posts: 8


My Contributions
I have a weird problem. If I just start a new windows application project, add a keypress event to the form so it looks like this:

CODE
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
        MessageBox.Show(e.KeyChar.ToString(), "Your input");
}


and it works. But if I try to do the same with one of my projects nothing happens when a key is pressed. Could something else be interfering with the event? I'm not using any other event handlers.
User is offlineProfile CardPM
+Quote Post

phatSolutions
RE: Keypress Event Not Working
27 Dec, 2007 - 08:53 AM
Post #2

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 12

QUOTE(kurent @ 27 Dec, 2007 - 09:31 AM) *

I have a weird problem. If I just start a new windows application project, add a keypress event to the form so it looks like this:

CODE
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
        MessageBox.Show(e.KeyChar.ToString(), "Your input");
}


and it works. But if I try to do the same with one of my projects nothing happens when a key is pressed. Could something else be interfering with the event? I'm not using any other event handlers.



in the other forms where this is not working are you wiring the event? Like so..

CODE
Form1.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);


Change Form1 to whatever the other form names are.
User is offlineProfile CardPM
+Quote Post

kurent
RE: Keypress Event Not Working
27 Dec, 2007 - 10:00 AM
Post #3

New D.I.C Head
*

Joined: 27 Dec, 2007
Posts: 8


My Contributions
Yes, it adds that automatically when you select the form in form designer and double click on the keypress event. That's why the whole thing is so puzzling. I do the exact same thing in my project as I do if I try to start from scratch in a new project.
User is offlineProfile CardPM
+Quote Post

kurent
RE: Keypress Event Not Working
28 Dec, 2007 - 07:03 AM
Post #4

New D.I.C Head
*

Joined: 27 Dec, 2007
Posts: 8


My Contributions
Seems this is a focus problem. If I delete all the buttons and text boxes it works.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Keypress Event Not Working
28 Dec, 2007 - 07:12 AM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You need to set the KeyPreview Property of your Form to True so it will recognize the key press event.

Hops that helps smile.gif
User is offlineProfile CardPM
+Quote Post

phatSolutions
RE: Keypress Event Not Working
28 Dec, 2007 - 08:18 AM
Post #6

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 12

QUOTE(PsychoCoder @ 28 Dec, 2007 - 08:12 AM) *

You need to set the KeyPreview Property of your Form to True so it will recognize the key press event.

Hops that helps smile.gif


Good catch..didn't think of that one.
User is offlineProfile CardPM
+Quote Post

kurent
RE: Keypress Event Not Working
28 Dec, 2007 - 10:49 AM
Post #7

New D.I.C Head
*

Joined: 27 Dec, 2007
Posts: 8


My Contributions
Yay now it works! Thank you!

I managed to read a 3D object file, draw it, rotate it...but using keyboard was beyond me haha.
User is offlineProfile CardPM
+Quote Post

phatSolutions
RE: Keypress Event Not Working
28 Dec, 2007 - 10:52 AM
Post #8

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 12

QUOTE(kurent @ 28 Dec, 2007 - 11:49 AM) *

Yay now it works! Thank you!

I managed to read a 3D object file, draw it, rotate it...but using keyboard was beyond me haha.


I hear ya, hate when seemingly easy tasks stump me but the complicated ones seem easy to figure out.

Glad it's working for you.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:59PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month