在文本框的keyup事件中按下后取消输入键

时间:2021-04-05 00:58:53

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

按返回键后如何取消文本框中的按键事件。

2 个解决方案

#1


4  

Set the Handled property of KeyPressEventArgs handler parameter to true.

将KeyPressEventArgs处理程序参数的Handled属性设置为true。

Example from msdn:

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.

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx。

#2


1  

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

你的意思是,你希望它忽略输入键吗?

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

你可以添加一个keydown事件并忽略那里的回车键......

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

#1


4  

Set the Handled property of KeyPressEventArgs handler parameter to true.

将KeyPressEventArgs处理程序参数的Handled属性设置为true。

Example from msdn:

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.

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx。

#2


1  

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

你的意思是,你希望它忽略输入键吗?

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

你可以添加一个keydown事件并忽略那里的回车键......

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