Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Handling Keypress events question

I am using Visual Studio 2005 trying to develop a c# application. I want to create a textbox that will eventually be a scrolling calculator that will look like this.
0.
100. +
400. =
500. *
100. =

50,000.

I am attempting to make it so that when i hit a function key it will perform an action and jump to the next line. Currently, I am unable to get my textbox to recognize any keyboard events. I placed this eventhandler into the InitializeComponent() contstructer in form1.design.cs.

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form1_keyPress);

I then have

private void form1_keyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 'e')
{
e.Handled = true;
}
}

in form1.cs

I put this in there just to test to see if when i hit the e character it will recognize that i'm doing that and it wont show up in the text box so i can add functionality to certain keys to perform certain mathematical operations. How do I get it to recognize certain keys?
[1209 byte] By [surgesm] at [2007-11-11 10:23:59]
# 1 Re: Handling Keypress events question
You've created a handler for the form's KeyPress event. If the textbox has the focus, you need to handle the textbox's KeyPress event. Unless you intend to allow the user to edit the calculator tape, you should probably use a non-editable control, such as a label or listbox, instead of a textbox.

Also, you may want to try http://www.google.com/search?q=c%23+calculator ;-)
Phil Weber at 2007-11-11 20:47:59 >