【问题标题】:Delay between key pressing in C#C#中按键之间的延迟
【发布时间】:2012-09-10 10:28:55
【问题描述】:

我有一个方法,它在 KeyPress 事件上搜索 DataGridView 中的一些数据,然后聚焦找到输入字符串的行(如果找到)。

private string input;
    private void Find_Matches()
    {            
        if (input.Length == 0) return;

        for (int x = 0; x < dataGridViewCustomers.Rows.Count; x++)
            for (int y = 0; y < dataGridViewCustomers.Columns.Count; y++)
                if (dataGridViewCustomers.Rows[x].Cells[y].Value.ToString().Contains(input))
                    dataGridViewCustomers.Rows[x].Selected = true;

     }

private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
    {            
        input += e.KeyChar.ToString();
        Find_Matches();
    }

如何计算按键之间的延迟,如果超过 1 秒,则清除“输入”字符串?这是不间断搜索所必需的。

谢谢。

【问题讨论】:

    标签: c# key


    【解决方案1】:

    利用System.Timers.Timer 是这样完成的:

    private Timer myTimer = new Timer(1000); //using System.Timers, 1000 means 1000 msec = 1 sec interval
    
    public YourClassConstructor()
    {
        myTimer.Elapsed += TimerElapsed;
    }
    
    private void TimerElapsed(object sender, EventArgs e)
    {
        input = string.Empty;
        myTimer.Stop();
    }
    
    // this is your handler for KeyPress, which will be edited
    private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if (myTimer.Enabled) myTimer.Stop(); // interval needs to be reset
        input += e.KeyChar.ToString();
        Find_Matches();
        myTimer.Start(); //in 1 sec, "input" will be cleared
    }
    

    【讨论】:

      【解决方案2】:

      你必须使用 System.Threading.Timer。 将回调传递给将清除输入的计时器。 每次触发 KeyPress 事件时,您都必须将计时器的间隔更新为 1000 毫秒

      timer.Change(0,1000);
      

      timer.Change(1000,0);
      

      我不记得 Change 方法的正确参数顺序,试试看

      【讨论】:

        【解决方案3】:

        您可以使用设置为 1 秒间隔的计时器,并在 KeyPress() 方法中重置计时器。 还要为它重置处理程序中的计时器。这将导致在自上次击键后经过一秒时调用计时器的处理程序。

        在计时器的处理程序中,在自上次击键后一秒钟后执行您需要执行的任何操作。

        我推荐使用这个计时器:http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

        您可以通过将 SynchronizingObject 设置为您的表单/控件来使其回调 UI 线程(我认为您会想要):

        http://msdn.microsoft.com/en-us/library/system.timers.timer.synchronizingobject.aspx

        【讨论】:

        • 这将导致与按键被按下时一样多的调用“当一秒钟过去后你需要做的事情”。
        • @gustavogbc 不,因为您在按下另一个键时重置计时器。因此,如果您在计时器触发之前按下另一个键,它将重置并且在最后一个键被按下后一秒钟后才会触发。请注意我所说的“在 KeyPress() 方法中重置计时器”。
        • @gustavogbc (通过重置,我的意思是先调用 Stop() 然后调用 Start())
        【解决方案4】:

        首先新建一个System.Windows.Forms.Timer,配置如下:

        _TimerFilterChanged.Interval = 800;
        _TimerFilterChanged.Tick += new System.EventHandler(this.OnTimerFilterChangedTick);
        

        然后在您的代码中添加以下方法:

        private void OnTimerFilterChangedTick(object sender, EventArgs e)
        {
            _TimerFilterChanged.Stop();
            Find_Matches();
        }
        

        您的按键事件处理程序应更改如下:

        private void dataGridViewCustomers_KeyPress(object sender, KeyPressEventArgs e)
        {            
            input += e.KeyChar.ToString();
            _TimerFilterChanged.Stop();
            _TimerFilterChanged.Start();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-25
          • 2012-07-18
          • 1970-01-01
          • 2011-05-19
          • 1970-01-01
          • 1970-01-01
          • 2011-09-04
          • 1970-01-01
          相关资源
          最近更新 更多