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.

private void calculator_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            textBox1.Text = "key pressed";

        }

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.

is that assigned to your forms keypress?

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.

From Form properties make Key preview property true, in case your answer to Lizr question is YES.

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/library/system.windows.forms.control.keypress.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.

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??

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.

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.

Thanks ddanbe and Lizr its working now!

I didn't realize you could do that. That makes it much easier.

cool - please mark it as solved

cool - please mark it as solved

Why are you so keen on having people mark threads as solved?

I frequently ask people to do so, because we can use it as reference or for people need to ask something, may search on them; instead making new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.