By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Form.KeyPress event

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.