.NET — Textbox control - wait till user is done typing

前端 未结 6 852
轻奢々
轻奢々 2020-12-03 05:29

Greetings all,

Is there a built in way to know when a user is done typing into a textbox? (Before hitting tab, Or moving the mouse) I have a database query that occ

6条回答
  •  隐瞒了意图╮
    2020-12-03 06:07

    For those who need something like this in .NET 2.0, here I made a Control that derives from TextBox and uses the same approach.. Hope this help

    public partial class TextBox : System.Windows.Forms.TextBox
    {
    
        private ManualResetEvent _delayMSE;
        public event EventHandler OnUserStopTyping;
        private delegate bool TestTimeout();
    
        public TextBox()
        {
            _delayMSE = new ManualResetEvent(false);
            this.TextChanged += new EventHandler(TextBox_TextChanged);
        }
    
        void TextBox_TextChanged(object sender, EventArgs e)
        {
    
    
            _delayMSE.Set();
            Thread.Sleep(20);
            _delayMSE.Reset();
    
            TestTimeout tester = new TestTimeout(TBDelay);
            tester.BeginInvoke(new AsyncCallback(Test), tester);
    
        }
    
    
        private void Test(IAsyncResult pResult)
        { 
            bool timedOut = (bool)((TestTimeout)pResult.AsyncState).EndInvoke(pResult);
            if (timedOut)
            {
                if (OnUserStopTyping != null)
                    OnUserStopTyping(this, null);
            }
        }
    
        private bool TBDelay()
        { 
            return !_delayMSE.WaitOne(500, false); 
        }
    
    }
    

提交回复
热议问题