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
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
}
}
Re: Text Box executes method after ENTER
Thanks mate.....
That works perfectly- much appreciated...
Cheers,
Paul.
Re: Text Box executes method after ENTER
No problem, I use it all the time. Its always been a beautiful solution :wave: