473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7250
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
1
by: ray well | last post by:
hi, i need to preview the keys in my app in order to process F1-F10. i set keypreview of my form to true, and it does capture the keystrokes from all over the forms controls which i then...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
13
by: Himselff | last post by:
Hi guys, Im new to dot net and wondering how i could remake the same way i was in vb6, capture all keypress whitin a form, per exemple i was on the form looking for some keypress privete...
4
by: polocar | last post by:
Hi, I would like to find a way in C# so that, when the user presses the "Esc" key, the form closes; the problem is that I have a lot of controls in the form. At the beginning I have tried with...
3
by: Luc The Perverse | last post by:
Hey - I am making my first C# form application! (More a teach myself app than any pratical application) I have a listbox that always has the control so I made an event handler for KeyDown. I...
4
by: Joergen Bech | last post by:
Just out of curiosity: What is your favorite method of making sure that anything that happens on a form, only happens in response to a single, external event? Take the example below. I have made...
4
by: Yoavo | last post by:
Hi, I want to close my form when the user presses the Escape key. I tried to catch the event KeyPress of the form but the program do not go through this code. I tried to catch the KeyPress event...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.