0

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();
        }
2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
Featured Replies
  • This will do (use key up event) [CODE] private void button1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.Close(); } } [/CODE] Read More

0

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

1

This will do (use key up event)

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

Edited by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.