简体   繁体   English

防止多次触发keydown事件C#

[英]Prevent multiple firing of keydown events C#

I want to allow the user to send his message when he press enter in the textbox. 我想允许用户在文本框中按Enter时发送消息。 I went to search and im using the sample codes below. 我去搜索和即时通讯使用下面的示例代码。

Now the problem is when i press enter, the event is triggered more than once like about 4-5 times. 现在的问题是,当我按Enter键时,该事件被触发了不止一次,大约是4-5次。

Someone else suggested to use keyup. 有人建议使用keyup。 I have tried keyup, keydown and keypress. 我已经尝试过按键,按键按下和按键。 All have the same problem. 都有相同的问题。 How do i prevent it from firing the event more than once? 如何防止它多次触发事件?

private void tbxAnswer_TextChanged(object sender, EventArgs e)
    {
        tbxAnswer.KeyUp += new KeyEventHandler(tbxAnswer_KeyUp);
    }

    private void tbxAnswer_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == (char)13)
        {

            MessageBox.Show("Hello");

        }
    }

Thank you! 谢谢!

This is because every time you change the text, the tbxAnswer_TextChanged is called/ fired you assign an action to the keyup event; 这是因为每次更改文本时,都会调用/触发tbxAnswer_TextChanged,您将一个操作分配给keyup事件。 if the text is changed 4 times then you assigned the keyup event 4 times and it increases every time you change the text. 如果文本更改了4次,则您分配了4次keyup事件,并且每次更改文本时该事件都会增加。 try this out: 试试这个:

tbxAnswer.KeyUp += new KeyEventHandler(tbxAnswer_KeyUp);
private void tbxAnswer_TextChanged(object sender, EventArgs e)
    {

    }

    private void tbxAnswer_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == (char)13)
        {

            MessageBox.Show("Hello");

        }
    }

You are adding the KeyUp event handler multiple times (inside the TextChanged handler); 您将多次添加KeyUp事件处理程序(在TextChanged处理程序内部); therefore, when Enter is pressed, the handler executes multiple times. 因此,当按下Enter键时,处理程序将执行多次。

What you want to do here is add the KeyUp handler just once, inside your form's constructor, just after the InitializeComponent() call: 您要在此处执行的操作是,在InitializeComponent()调用之后,仅在窗体的构造函数中添加一次KeyUp处理程序:

public MyForm()
{
    // other code possibly here
    InitializeComponent();

    // and now add the event handler:
    tbxAnswer.KeyUp += new KeyEventHandler(tbxAnswer_KeyUp);
}

private void tbxAnswer_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyValue == (char)13)
    {
        MessageBox.Show("Hello");
    }
}

Change your code to this 将您的代码更改为此

tbxAnswer.KeyUp -= tbxAnswer_KeyUp;
tbxAnswer.KeyUp += new KeyEventHandler(tbxAnswer_KeyUp);

In your code snippet, whenever the text of the TextBox changes, another eventhandler is added to the KeyUp handler. 在您的代码段中,每当TextBox更改时,就会将另一个事件处理程序添加到KeyUp处理程序中。 You should only add event handlers once (for instance, just after creating the textbox). 您仅应添加一次事件处理程序(例如,仅在创建文本框之后)。

Sara and Jon have already provided the correct answer to your specific question. Sara和Jon已经为您的特定问题提供了正确的答案。 But if you want to go further and get a better understanding of how and when to use any particular key handling event take a look at my article Exploring Secrets of .NET Keystroke Handling . 但是,如果您想走得更远并且对如何以及何时使用任何特定的键处理事件有更好的了解,请看一下我的文章“ 探索.NET键盘处理的秘密” I explain and diagram when and where each event is useful, plus provide a KeystrokeSandbox application that lets you actually watch what happens! 我将说明并绘制每个事件在何时何地有用的图表,并提供一个KeystrokeSandbox应用程序,让您实际观察会发生什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM