Keypress not working

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 4
Reputation: OrderChaos is an unknown quantity at this point 
Solved Threads: 0
OrderChaos OrderChaos is offline Offline
Newbie Poster

Keypress not working

 
0
  #1
Jan 27th, 2009
Hello I am working on a simple calculator program for school. I already have all the button clicks working, which is all that was required.

I thought it would be cool if I could get it to where you could use the keyboard for entry and I've done similar things with Visual Basic (which I learned in high school).

I looked online and found the syntax (I'm pretty sure its right) but for some reason it's not sensing a keypress event.

This is the code I have right now, I just want to get the keypress working, I already know how to do the specifics inside the event once it is working.

Oh and I do have keypreview enabled on my form.
  1. private void calculator_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
  2. {
  3. textBox1.Text = "key pressed";
  4.  
  5. }

Thanks in advance for the help and like I said, Its already done as far as the assignment was, I just want to learn how to do this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Keypress not working

 
0
  #2
Jan 27th, 2009
is that assigned to your forms keypress?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: nelis is an unknown quantity at this point 
Solved Threads: 7
nelis nelis is offline Offline
Newbie Poster

Re: Keypress not working

 
0
  #3
Jan 27th, 2009
Yea something is odd about your code. If that event method was generated by visual studio, why does it have System.Windows.Forms.KeyEventArgs in the parameters definition. To me this makes me think that Windows.Forms isn't in the namespace which would be odd if that code is in the form.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Keypress not working

 
0
  #4
Jan 27th, 2009
From Form properties make Key preview property true, in case your answer to Lizr question is YES.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: OrderChaos is an unknown quantity at this point 
Solved Threads: 0
OrderChaos OrderChaos is offline Offline
Newbie Poster

Re: Keypress not working

 
0
  #5
Jan 27th, 2009
Keypreview is enabled, I think I said that in my original post, but maybe not. Anyway I am using visual c# express to create this and I got the code for the keypress by using MSDN Here: http://msdn.microsoft.com/en-us/libr...ss.aspx?ppud=4

I couldn't find a way to automatically add in a keypress in visual express. I manually added in the method based upon the msdn example. I am still new to this program, so maybe I just haven't found it yet.

If this isn't what you meant, please let me know and if you know a way to add in the keypress method without manually typing it please let me know.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Keypress not working

 
0
  #6
Jan 27th, 2009
I guess changing from the properties to events in the bottom right (by default) was missing..
So.. Im gonna take an educated guess you didnt tie the event to the method you made??
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: OrderChaos is an unknown quantity at this point 
Solved Threads: 0
OrderChaos OrderChaos is offline Offline
Newbie Poster

Re: Keypress not working

 
0
  #7
Jan 27th, 2009
I'm not entirely sure what you mean, considering I am still new to making forms in c# (I was introduced to the language last semester where all we did was command line programs) but I think I know what you mean.

If I understand you correctly there is something I have to do to get the method to be called whenever a key is pressed. If so then you are correct I don't have this.

Also the only thing in the bottom (right or left) is the error list when in code view and the properties window in design view.

Btw thanks for the quick reply.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,054
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 311
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: Keypress not working

 
0
  #8
Jan 27th, 2009
When yo are in design view and you press the little button with the lightning symbol on it, in the properties window. You get a list of all available events for the control currently selected. Double click the event you want and you will get into the code view in the eventhandler you just doubleclicked. Type in the code you want to do, it will work. The grungy details of the event handling are taken care of in the in the forms.designer.cs class.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: OrderChaos is an unknown quantity at this point 
Solved Threads: 0
OrderChaos OrderChaos is offline Offline
Newbie Poster

Re: Keypress not working

 
0
  #9
Jan 27th, 2009
Thanks ddanbe and Lizr its working now!

I didn't realize you could do that. That makes it much easier.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Keypress not working

 
0
  #10
Jan 27th, 2009
cool - please mark it as solved
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 1649 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC