-
March 12th, 2009, 10:12 AM
#1
Text Box executes method after ENTER
Hi All,
I have a text box in my application. I want my program to execute a method only when the user has keyboarded "return". At present I have it working when the user edits the text in the text box, the method is called but I want it only to call the method after the user has pressed return?
Can you help me with this?
I am using Visual C#2008 to build my application.
Thanks in advance.......
Paul
-
March 12th, 2009, 11:03 AM
#2
Re: Text Box executes method after ENTER
Handle the TextBox.KeyDown event, and then check the e.KeyCode property for Keys.Enter. If the user pressed enter, call e.SuppressKeyPress = true to prevent that ding sound when the key is pressed and then call the method you want to call. Example:
Code:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true; // Prevent the ding sound and stop the message pump
MyMethodCall(); // Make the call to the method you want to run
}
}
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
March 12th, 2009, 12:21 PM
#3
Re: Text Box executes method after ENTER
Thanks mate.....
That works perfectly- much appreciated...
Cheers,
Paul.
-
March 12th, 2009, 12:33 PM
#4
Re: Text Box executes method after ENTER
No problem, I use it all the time. Its always been a beautiful solution
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|