简体   繁体   中英

TextBox events in c# on press enter

Which event do I need to use when user write some value in text Box, and after pressing enter to execute something?

I need when user write numbers and when he press enter to calculate something in another method.

Use KeyDown event like this:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
         MessageBox.Show("Test"); // Or call a method
    }
}

When you set the AcceptButton in the form you should also set the AcceptButton property to null when enter TextBox and reassign the button to it when Leave the TextBox like this:

private void textBox1_Leave(object sender, EventArgs e)
{
    AcceptButton = button1;
}

private void textBox1_Enter(object sender, EventArgs e)
{
    AcceptButton = null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM