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?
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
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
Bookmarks