【问题标题】:How to know if an event was called there for three seconds?如何知道一个事件是否在那里被调用了三秒钟?
【发布时间】:2012-02-08 19:29:24
【问题描述】:

如果用户在 3 秒内没有在文本框中输入任何内容,我正在寻找一个代码示例/算法来执行操作。

我的风景:我有一个textbox,如果文本框有焦点,我想提交它,但用户在 3 秒内没有写任何东西。这可能吗?

【问题讨论】:

  • 那么当文本框获得焦点开始计数,并在三秒钟后通过执行其他操作进行干预?你问的是这个吗?
  • 你使用的是winforms还是wpf?
  • @Yuck: ...如果用户在三秒内开始做某事,不要忘记取消其他操作。

标签: c# .net events controls


【解决方案1】:

您可以在TextChange 中设置一个事件,将当前时间存储到一个变量中,以便对文本框进行任何更改。

然后你可以添加一个每秒触发一次的Timer。计时器可以检查文本框是否有焦点,如果有,变量中的时间戳是否超过三秒(可能文本框是否为空),如果是,请调用您的提交方法。

【讨论】:

    【解决方案2】:

    您需要设置一个System.Windows.Forms.Timer 计时器。每当文本框中的文本发生变化时(TextChanged 事件),您需要重新启动计时器以在 3 秒内触发。如果计时器触发,这意味着用户没有输入任何内容时已经过去了 3 秒。

    但是,请注意,这是代表您的应用程序的非常奇怪的行为,而且任何用户都不太可能对此表示赞赏。

    【讨论】:

      【解决方案3】:

      我没有现成的示例,但我相信您可以使用Timer 来完成此操作。将超时设置为 3000 毫秒,当用户键入文本框时将其重置(使用 TextChanged 或等效事件,仅 KeyPress 不会在右键单击菜单剪贴板粘贴等时触发),并在计时器处理程序中,禁用计时器(以确保它不会重复触发)并执行您喜欢的任何逻辑。另外,根据文本框是否有焦点来启用/禁用计时器。

      【讨论】:

        【解决方案4】:

        这里是如果用户没有移动鼠标 3 秒如何隐藏光标的示例。 你必须使用 TextChange 事件来做类似的事情。

        private DispatcherTimer CursorTimer { get; set; }
        private DateTime CursorLastMoveTime { get; set; }
        
        void CursorTimer_Tick(object sender, EventArgs e)
                {
                    TimeSpan delta = DateTime.Now.Subtract(this.CursorLastMoveTime);
                    if (delta.TotalSeconds > 3)
                    {
                        CursorTimer.Stop();
                        Mouse.OverrideCursor = Cursors.None;
                    }
                }
        
        
        private void Window_MouseMove(object sender, MouseEventArgs e)
                {
                    #region Hide/Show cursor over the main window
                    Mouse.OverrideCursor = Cursors.Arrow;
                    CursorLastMoveTime = DateTime.Now;
                    if (CursorTimer.IsEnabled == false)
                        CursorTimer.Start();
                    #endregion
                }
        

        【讨论】:

          【解决方案5】:

          您可以使用间隔为 3000 毫秒的计时器。如果间隔已过,则会触发一个事件,如果用户在您的文本框中输入文本,则会重置计数。

          public partial class Form1 : Form
          {
              System.Timers.Timer timer;
          
              public Form1()
              {
                  InitializeComponent();
          
                  timer = new System.Timers.Timer();
                  timer.Interval = 3000;
                  timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
          
                  textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
                  textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
                  textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
              }
          
              void textBox1_LostFocus(object sender, EventArgs e)
              {
                  timer.Stop();
              }
          
              void textBox1_GotFocus(object sender, EventArgs e)
              {
                  timer.Start();
              }
          
              void textBox1_KeyPress(object sender, KeyPressEventArgs e)
              {
                  timer.Stop();
                  timer.Start();
              }
          
              void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
              {
                  MessageBox.Show("You have not entered text in the last 3 seconds!");
              }
          }
          

          【讨论】:

          • 正如其他人所说,请注意 TextChanged 事件是更好的选择,而不是本例中的 KeyPress。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-19
          • 1970-01-01
          • 1970-01-01
          • 2014-10-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多