Whay Does KeyDown event not cancel a key stroke

O

orekinbck

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
 
C

cody

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top