473,480 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overriding Key Strokes

I need to override the Enter key for the entire form. In my Windows form,
when a user presses Enter, I want it to behave just like a Tab, except when
the focus is on a button. Here are the 2 methods I've tried:

public EstDefaults()
{
InitializeComponent();

KeyPress += new KeyPressEventHandler(EstDefaults_KeyPress);
}
private void EstDefaults_KeyPress(object sender, KeyPressEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused)
{
e.KeyChar = (char)9;
}
}
AND
public EstDefaults()
{
InitializeComponent();

KeyDown += new KeyEventHandler(EstDefaults_KeyDown);
}

private void EstDefaults_KeyDown(object sender, KeyEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused )
{
e.KeyCode = Keys.Tab;
}
}
The KeyPress does nothing, and the KeyDown won't compile because e.KeyCode
is read-only. KeyPreview is set to True at the form level.

What am I doing wrong? Thanks!
Nov 1 '06 #1
2 1686
Use keypress. If the key is "enter", set e.handled to "true". That
will make the control ignore the "enter" event. Then you can use the
GetNextControl().Focus or SelectNextControl methods (not sure which) to
handle activating the next control.

Irvin McCoy wrote:
I need to override the Enter key for the entire form. In my Windows form,
when a user presses Enter, I want it to behave just like a Tab, except when
the focus is on a button. Here are the 2 methods I've tried:

public EstDefaults()
{
InitializeComponent();

KeyPress += new KeyPressEventHandler(EstDefaults_KeyPress);
}
private void EstDefaults_KeyPress(object sender, KeyPressEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused)
{
e.KeyChar = (char)9;
}
}
AND
public EstDefaults()
{
InitializeComponent();

KeyDown += new KeyEventHandler(EstDefaults_KeyDown);
}

private void EstDefaults_KeyDown(object sender, KeyEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused )
{
e.KeyCode = Keys.Tab;
}
}
The KeyPress does nothing, and the KeyDown won't compile because e.KeyCode
is read-only. KeyPreview is set to True at the form level.

What am I doing wrong? Thanks!
Nov 1 '06 #2
Thanks, I'll try it.

If it ignores the Enter key, will it still go through the validation process?

"Martin Z" wrote:
Use keypress. If the key is "enter", set e.handled to "true". That
will make the control ignore the "enter" event. Then you can use the
GetNextControl().Focus or SelectNextControl methods (not sure which) to
handle activating the next control.

Irvin McCoy wrote:
I need to override the Enter key for the entire form. In my Windows form,
when a user presses Enter, I want it to behave just like a Tab, except when
the focus is on a button. Here are the 2 methods I've tried:

public EstDefaults()
{
InitializeComponent();

KeyPress += new KeyPressEventHandler(EstDefaults_KeyPress);
}
private void EstDefaults_KeyPress(object sender, KeyPressEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused)
{
e.KeyChar = (char)9;
}
}
AND
public EstDefaults()
{
InitializeComponent();

KeyDown += new KeyEventHandler(EstDefaults_KeyDown);
}

private void EstDefaults_KeyDown(object sender, KeyEventArgs e)
{
if (!exit.Focused || !cancel.Focused || !Delete.Focused )
{
e.KeyCode = Keys.Tab;
}
}
The KeyPress does nothing, and the KeyDown won't compile because e.KeyCode
is read-only. KeyPreview is set to True at the form level.

What am I doing wrong? Thanks!

Nov 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3769
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
3
4148
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
6
4910
by: Al Bahr | last post by:
H I need to combine Ink strokes with the background image into a bit map. Would appreciate any help Thank A
3
1517
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
3
5635
by: Paulers | last post by:
Hello, I need to emulate keyboard strokes from a console application. The console application monitors a textfile and when something is matched in a text file I need the matched string outputted...
1
1393
by: sreenulanka | last post by:
I have five panel each one layout is set to gridbag layout.and also i have one main panel, layout is box layout.all components add to container.my page is larger than screen.it is not moving with...
0
1345
by: abcd | last post by:
Is there a way to capture key strokes on a system using python (other than pyHook)? can wxPython capture keystrokes for the system (not just, say a text box)? thanks
4
3712
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I am new to C#. I read two C# books in the last two days which only covered the basics. And now I am on my third day. The books were really bad, because they did not cover windows applications,...
10
104939
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
11
10540
by: shadyabhi | last post by:
How can i accept key strokes like Ctrl+A etc etc ... i mean combination of 2 keys... Pls give a small peice of code to demonstrate... i use gcc under linux platform
0
7055
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
6920
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
7060
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
7106
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...
0
5365
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,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
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...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.