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)

private void TESTER_KeyDown(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                MessageBox.Show("Test Complete");
            }

        }

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.

Edited 7 Years Ago by Moselekm: n/a

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

I have no problem with the following snippet with a breakpoint on the return statement...

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

If you are still having trouble, zip up your project and attach it.

Edited 7 Years Ago by DdoubleD: n/a

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.

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

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                MessageBox.Show("RETURN key pressed");
        }

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.

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);

This question has already been answered. Start a new discussion instead.