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

Balamurugan Thirumalaikumar 1 Reputation point
2021-01-22T05:37:22.447+00:00

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,884 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-01-22T07:29:59.807+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.