473,408 Members | 1,743 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,408 software developers and data experts.

Whay Does KeyDown event not cancel a key stroke

Hi There

In C# windows app, .NET 2003 I have a text box with the following event
handler:

private void textBox3_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
e.Handled = true;
}

I would expect that the text box would not let me type a but it does
?!?!

However doing something very similar in a KeyPress event handler works
fine ?!?

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
e.Handled = true;
}

Why doesn't the code in the KeyDown event handler stop the user typing
in an a ?

TIA
Bill

Nov 17 '05 #1
2 12183
Hi,

Using KeyPress is the way to go.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<or*******@yahoo.com.au> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi There

In C# windows app, .NET 2003 I have a text box with the following event
handler:

private void textBox3_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
e.Handled = true;
}

I would expect that the text box would not let me type a but it does
?!?!

However doing something very similar in a KeyPress event handler works
fine ?!?

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
e.Handled = true;
}

Why doesn't the code in the KeyDown event handler stop the user typing
in an a ?

TIA
Bill

Nov 17 '05 #2
because thew keydown event is processed after the control processes its
keys.
you should overrride the method processcmdkeys() in the textbox and capture
the keys there.

<or*******@yahoo.com.au> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi There

In C# windows app, .NET 2003 I have a text box with the following event
handler:

private void textBox3_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
e.Handled = true;
}

I would expect that the text box would not let me type a but it does
?!?!

However doing something very similar in a KeyPress event handler works
fine ?!?

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
e.Handled = true;
}

Why doesn't the code in the KeyDown event handler stop the user typing
in an a ?

TIA
Bill

Nov 17 '05 #3

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

Similar topics

4
by: Fred Heida | last post by:
Hi All, maybe this is a sill question..but if i have TextBox and use the SendMessage(..) to send a character the control.. the KeyDown event is not triggered.... in would need to check in the...
4
by: Tony | last post by:
I'm building a simple "breakout" style video-game, and I want keyboard control of the paddle. What I need is for an event handler to respond whenever a key is pressed, regardless of what the...
4
by: Anne | last post by:
hie again, i have 3 textbox and i would like the user to go to the next textbox by pressing the 'ENTER' key. i have tried using this: Private Sub txtRequestor_KeyDown(ByVal sender As...
2
by: George Durzi | last post by:
I have a text box and a button, and I want the enter key to run the click event of the button. The textbox and button are inside a user control. I tried all sorts of stuff with the __EVENTTARGET...
0
by: Kartic | last post by:
I have a ComboBox with DropDownStyle=DropDownList. Is it possible to cancel the e.KeyCode in KeyDown event. When I press "N" it goes to Item starting with N, which is ok. But when I press CTL+N...
2
by: akameswaran | last post by:
Admittedly this problem causes no actual functional issues aside from an occasional error message when the program exits. The error is: Unhandled exception in thread started by Error in...
5
by: ApexData | last post by:
This follows a previous post, when I was trying to capture a key pressed during the immediate opening of a form (ie in the first 3-secs before processing any of the code that followed it. It was...
1
by: kgerritsen | last post by:
I am building an application that will receive input from a barcode scanner. The barcode scanner is configured to append to the front value a single character and hyphen that identify the barcode...
9
by: pvsundarram | last post by:
hey, i am trying to cancel the keydown event for certain keycodes( for eg:- enter key ).But the cancelling of this event is not happening in firefox. Is there any way to cancel the event in the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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...

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.