Help with KeyPress?!

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 9
Reputation: Moselekm is an unknown quantity at this point 
Solved Threads: 0
Moselekm Moselekm is offline Offline
Newbie Poster

Help with KeyPress?!

 
0
  #1
26 Days Ago
Alright, I have been struggling with this for a while. I just sort of want to grasp and test out everything I have learned, but that's hard to do when they don't seem to work.

Basically all I want to do is cause an event [MessageBox] when I hit a key. In this case, Enter. I think the event will be very helpful with a lot of the apps I plan on making. I have searched all over and on here and used all the examples, but nothing works.

Here is what I got:
(Tester is the TEXTBOX)
  1. private void TESTER_KeyDown(object sender, KeyPressEventArgs e)
  2. {
  3. if (e.KeyChar == 13)
  4. {
  5. MessageBox.Show("Test Complete");
  6. }
  7.  
  8. }

I can build and run fine, but that instance never takes. I put the code in both Form.cs and Designer.cs. Nothing. I am not sure what I am doing wrong. Everytime I read on a forum when they attempt this code and ask for help, they get the above as a solution and it works for them.

Notes:
-I have KeyPreview set to True.
-I have tried KeyDown.
-I have tried (char)Keys.Enter.
-I have tried other ASCII keys.
Last edited by Moselekm; 26 Days Ago at 10:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 920
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
26 Days Ago
If the form's AcceptButton property is set to other than "none", then the form will intercept the <RETURN> key and transfer control to the specified event... Check that the Form's property is not set: (eg. this.AcceptButton = this.button1; )--be sure to look in the form's designer.cs file...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: Moselekm is an unknown quantity at this point 
Solved Threads: 0
Moselekm Moselekm is offline Offline
Newbie Poster
 
0
  #3
26 Days Ago
There is no Button on the form. Just the textbox.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 920
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #4
26 Days Ago
I have no problem with the following snippet with a breakpoint on the return statement...

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. if (e.KeyChar == 13)
  4. return; // breakpoint here always halts process for me...
  5. }

If you are still having trouble, zip up your project and attach it.
Last edited by DdoubleD; 26 Days Ago at 12:14 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: Moselekm is an unknown quantity at this point 
Solved Threads: 0
Moselekm Moselekm is offline Offline
Newbie Poster
 
0
  #5
26 Days Ago
Okay probably a newb question, but what value is it returning? A bool?

Also, the instance I want to happen is when enter is pressed a message box pops up.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 920
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #6
26 Days Ago
Originally Posted by Moselekm View Post
Okay probably a newb question, but what value is it returning? A bool?

Also, the instance I want to happen is when enter is pressed a message box pops up.
Display messagebox...

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. if (e.KeyChar == 13)
  4. MessageBox.Show("RETURN key pressed");
  5. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #7
26 Days Ago
Add a handler for KeyPress event by double clicking on event name KeyPress at Properties windows and put the code inside the keypress handler suggested by Mr. DdoubleD.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 378
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 68
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
0
  #8
26 Days Ago
Originally Posted by adatapost View Post
Add a handler for KeyPress event by double clicking on event name KeyPress at Properties windows and put the code inside the keypress handler suggested by Mr. DdoubleD.
Almost word for word what i was about to type :p

If you are new to C# then this is a concept you will encounter often.
The code you are trying to use is an event handler, in order for the method to execute when the event is raised you need to tell the application to lsiten for the event and point it to the method. Event handlers can be created in the designer (check out the tutorial here) or in code. In code they look like this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Reply

Tags
c#, keypress

Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC