Sytem.Windows.Forms.TextBox Keydown Event e.handled not working as expected.

Balamurugan Thirumalaikumar 1 Reputation point

Hi MSDN Forums,

We have checked Sytem.Windows.Forms.TextBox Keydown Event when we press a backspace it working(selected text deleted) for both e.handled=true and e.handled=false and also please let us know how to handle the textbox backspace key and A key.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points

    Hi BalamuruganThirumalaikumar-1818,
    The Handled property is used to notify the base class whether the event has been handled or whether they need to resolve the event. It can only be used in the context of the Form.KeyPress event.
    You need to use SuppressKeyPress in KeyDown event.

    private void textBox1_KeyDown(object sender, KeyEventArgs e)  
        if (e.KeyCode==Keys.Back || e.KeyCode==Keys.A)  
            e.SuppressKeyPress = true;  

    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments