C # text box

I have a standard text box that I want to execute to press a key. I have this code currently:

private void idTextEdit_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter/Return)
        {
            e.Handled = true;
            SearchButtonClick(sender, EventArgs.Empty);
        }
    }

The problem is that I tried both Enter and Return there, which is the reason for this. This is only a shooting that checks for normal keys that do not look like shift, control, etc. How can I create this so that it takes and uses the input / return key in the same way?

+3
source share
2 answers

Instead, use the event KeyDown:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Return)
    {
        //...
    }
}

If for some reason this should be KeyPress, you can use (char)13either '\r'for your verification, although I doubt it will work well on non-Windows OS.

if (e.KeyChar == '\r')

Keys.Return char, bitflag ASCII.

+6

KeyDown.

0

All Articles