Sunday, February 13, 2011

eliminate the enter key after pressing it in the keyup event of a textbox.

How to cancel a keypress event in a textbox after pressing the return key.

  • Set the Handled property of KeyPressEventArgs handler parameter to true.

    Example from msdn:

    private void keypressed(Object o, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            e.Handled = true;
        }
    }
    

    See http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx for more info.

    From lacop
  • Do you mean, you want it to ignore the enter key ?

    You could add a keydown event and ignore the enter key there...

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
            }
        }
    

0 comments:

Post a Comment