| | |
Help with KeyPress?!
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 9
Reputation:
Solved Threads: 0
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)
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.
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)
c Syntax (Toggle Plain Text)
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.
Last edited by Moselekm; 26 Days Ago at 10:11 pm.
•
•
Join Date: Jul 2009
Posts: 920
Reputation:
Solved Threads: 147
0
#4 26 Days Ago
I have no problem with the following snippet with a breakpoint on the return statement...
If you are still having trouble, zip up your project and attach it.
C# Syntax (Toggle Plain Text)
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.
Last edited by DdoubleD; 26 Days Ago at 12:14 am.
•
•
Join Date: Jul 2009
Posts: 920
Reputation:
Solved Threads: 147
0
#6 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.
C# Syntax (Toggle Plain Text)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) MessageBox.Show("RETURN key pressed"); }
0
#8 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.
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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- confused with if(KEY_DOWN(VK_UP)). keypress in genral. (C++)
- keypress detection (C)
- Keypress Detection (C)
Other Threads in the C# Forum
- Previous Thread: socket exception
- Next Thread: C# Random names
| Thread Tools | Search this Thread |
.net 2008 access activedirectory ado.net algorithm analyst app array asp.net basic beginner bot c# c++ check client combobox contorl data database datastructure datetime dbconnection default developer development dialog directrobot disabled dll drawing e-commerce editor error exectuable expression file foreach forloop form formbox forms function game gdi+ httpwebrequest index javascript keypress linux lisp list listbox mailmerge messagebox mono msword mysql operator panel photoshop php platform post programmer programming read redirect remote remoting reporting resourcefile richtextbox save saving smoobjects sql sql-server sqlserver statistics study table taborder text textbox usercontrol using-keyword validate vb.net video view visual visualstudio wia windows winforms wordautomation wpf xml







