Whay Does KeyDown event not cancel a key stroke

  • Thread starter Thread starter orekinbck
  • Start date Start date
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
 
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.
 
Back
Top