469,635 Members | 2,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,635 developers. It's quick & easy.

Form.KeyPress event

Hi,

I am trying to trace down the Enter key in my Form.KeyPress event handler.
The KeyPreview property is set to false, so I'd assume that all key presses
should go through my form's KeyPress event handler, right? Ok. If the focus
is set to any other control than a button, then the above event handler
fires, but if the focus is on a button, then I've no way to know that the
Enter key has been pressed or not, because in that case pressing Enter
doesn't fire KeyPress event of the Form. My question is Why?

Thanks for your help,

Nov 22 '05 #1
3 7051
Hello,

Try setting the Form.KeyPreview to true then add an Eventhandler to the
forms KeyUp event, this should capture the Enter-key.

"Nikolay Evseev" wrote:
Hi,

I am trying to trace down the Enter key in my Form.KeyPress event handler.
The KeyPreview property is set to false, so I'd assume that all key presses
should go through my form's KeyPress event handler, right? Ok. If the focus
is set to any other control than a button, then the above event handler
fires, but if the focus is on a button, then I've no way to know that the
Enter key has been pressed or not, because in that case pressing Enter
doesn't fire KeyPress event of the Form. My question is Why?

Thanks for your help,

Nov 22 '05 #2
Sorry, I should've said that KeyPreview is set to TRUE. Now, I was able to
capture the Return key using KeyUp event, but the button still gets pressed
even after setting e.Handled to TRUE. Do you know how to prevent this?

"Fredrik Johansson" wrote:
Hello,

Try setting the Form.KeyPreview to true then add an Eventhandler to the
forms KeyUp event, this should capture the Enter-key.

"Nikolay Evseev" wrote:
Hi,

I am trying to trace down the Enter key in my Form.KeyPress event handler.
The KeyPreview property is set to false, so I'd assume that all key presses
should go through my form's KeyPress event handler, right? Ok. If the focus
is set to any other control than a button, then the above event handler
fires, but if the focus is on a button, then I've no way to know that the
Enter key has been pressed or not, because in that case pressing Enter
doesn't fire KeyPress event of the Form. My question is Why?

Thanks for your help,

Nov 22 '05 #3
Hello,

I guess you _COULD_ override the ProcessDialogKey method to disable the
Enter-key for a button (but sill allow it to be clicked):

private void button1_Click(object sender, EventArgs e) {
// button clicked
Debug.WriteLine("BClick");
}

protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Enter) {
//TODO: something meaningful
Debug.WriteLine("Something meaningful");

if (this.ActiveControl == button1) {
// don't send the enter to the button
return false;
}
}
return base.ProcessDialogKey (keyData);
}

Hope it helps!

"Nikolay Evseev" wrote:
Sorry, I should've said that KeyPreview is set to TRUE. Now, I was able to
capture the Return key using KeyUp event, but the button still gets pressed
even after setting e.Handled to TRUE. Do you know how to prevent this?

"Fredrik Johansson" wrote:
Hello,

Try setting the Form.KeyPreview to true then add an Eventhandler to the
forms KeyUp event, this should capture the Enter-key.

"Nikolay Evseev" wrote:
Hi,

I am trying to trace down the Enter key in my Form.KeyPress event handler.
The KeyPreview property is set to false, so I'd assume that all key presses
should go through my form's KeyPress event handler, right? Ok. If the focus
is set to any other control than a button, then the above event handler
fires, but if the focus is on a button, then I've no way to know that the
Enter key has been pressed or not, because in that case pressing Enter
doesn't fire KeyPress event of the Form. My question is Why?

Thanks for your help,

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by afatdog | last post: by
reply views Thread by afatdog | last post: by
1 post views Thread by ray well | last post: by
13 posts views Thread by Himselff | last post: by
3 posts views Thread by Luc The Perverse | last post: by
4 posts views Thread by Yoavo | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.