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();
        }

Recommended Answers

All 5 Replies

try to change Return with Enter

if (e.KeyChar == (char)Keys.Enter) 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();
            }
        }

it works just fine, thanks a lot

I`m glad it does.
btw, please mark the thread as salved, if you got the answer on your question.
thx
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.