473,750 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7277
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 ProcessDialogKe y method to disable the
Enter-key for a button (but sill allow it to be clicked):

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

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

if (this.ActiveCon trol == button1) {
// don't send the enter to the button
return false;
}
}
return base.ProcessDia logKey (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
288
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
0
1221
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
1
3225
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 process. i found 2 puzzeling exception: if the user is inside the text box of a ComboBox, the keystrokes aren't captured in the form's keypress event. i have to separately capture the
1
6180
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 the Form.KeyPress event does not trigger anymore. A way to 'solve' this is to set the focus to the form, but during testing it happens ofter the keypress event is not getting triggered on keys like
13
6087
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 sub form_keypressed(keyascii as integer) if keyascii = 13 then 'for the enter key call button_click
4
10990
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 the following form event handler: protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e);
3
7888
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 seem to be losing information when Casting to char, since I never have any problem using special keys if I use something like e.KeyCode == Keys.Delete But . . there is no Keys.Astericks (or Star or similar)
4
2142
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 it as simple as I could: Put two textboxes on a form, paste the code and run it. The idea is that whatever is typed in one box is displayed in the other box, only reversed. This is a simple example of two repre- sentations of the same data on...
4
16418
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 for several conrtrols in the dialog and the event was caught, but I do not want to catch this event for every control in the form. What is the right way to close the form when the user presses Escape ?
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.