hi, I'm just trying to make the Cancel button on my form close that form on Enter key
but the problem is that it closes it when i hit space key and if i change code below to

if (e.KeyChar == (char)Keys.Return) this.Close();

it does not work.
it just works for the space key.
any help would be usefull.

private void Cancel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Space) this.Close();
        }

i tried, it just doesn't react on any other keys except for space, i do not know why

This will do (use key up event)

private void button1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Close();
            }
        }

Edited 5 Years Ago by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.