获取文本框的值,如果15秒后,最后一次更改c#

本文关键字:最后一次 15秒 如果 取文本 获取 | 更新日期: 2023-09-27 18:04:16

如何在最后一次更改后1秒获得文本框的值

我尝试秒表和时间戳,但我只是得到时间之间的两个变化,我不知道如何得到1秒后的文本框的值。

谢谢你的帮助!

编辑:

   Stopwatch TimerBetweenWrite = new Stopwatch();
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TimerBetweenWrite.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = TimerBetweenWrite.Elapsed;
        if (Search.Text != null && ts.Seconds >= 1)
        {
            //doing my stuff
        }
        TimerBetweenWrite.Restart();
   }

但这并不像我想要的那样工作,因为我们需要在最后一次更改后1秒更改TextBox。我想运行一个函数1秒后的最后一次更改的文本框,但用户可以继续更改文本框。

最后编辑:

那是工作的代码谢谢大家的帮助!

public partial class ViewerPage : Page
{
    System.Timers.Timer myTimer = new System.Timers.Timer(1000);
    public ViewerPage()
    {
         InitializeComponent();
         myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
    }
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        myTimer.Stop(); //Reset timer
        myTimer.Start(); //Restart it
     }
     private void myTimer_Elapsed(Object sender, ElapsedEventArgs e)
     {
         ThreadContext.InvokeOnUiThread(
         delegate()
         {
             // Doing My Stuff 
             myTimer.Stop();
          });
     }
}
public static class ThreadContext
{
    public static void InvokeOnUiThread(Action action)
    {
        if (Application.Current.Dispatcher.CheckAccess())
        {
            action();
         }
         else
         {
             Application.Current.Dispatcher.Invoke(action);
         }
     }
     public static void BeginInvokeOnUiThread(Action action)
     {
         if (Application.Current.Dispatcher.CheckAccess())
         {
             action();
         }
         else
         {
             Application.Current.Dispatcher.BeginInvoke(action);
         }
     }
}

获取文本框的值,如果15秒后,最后一次更改c#

Timer myTimer = new Timer(1000);
myTimer.Elapsed += myTimer_Elapsed;
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    myTimer.Stop(); //Reset timer
    myTimer.Start(); //Restart it
}
private void myTimer_Elapsed(Object sender, ElapsedEventArgs e)
{
    //Do your stuff
}

解释:每次文本改变时,计时器被重置并重新启动。只有当它被启用(也就是没有被TextChanged事件停止)一秒钟时,它才会打勾。如果您希望它只勾选一次然后停止,请将AutoReset属性设置为true。

您可以从TextBox继承并引发自己的StableTextChanged事件。新控件将出现在工具箱的顶部:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void textBoxEx1_StableTextChanged(object sender, EventArgs e)
    {
        label1.Text = ((TextBoxEx)sender).Text;
    }
}
public class TextBoxEx : TextBox
{
    public event dlgStableTextChanged StableTextChanged;
    public delegate void dlgStableTextChanged(object sender, EventArgs e);
    private System.Windows.Forms.Timer tmr;
    public TextBoxEx()
    {
        tmr = new System.Windows.Forms.Timer();
        tmr.Interval = 1000;
        tmr.Tick += Tmr_Tick;
        this.TextChanged += TextBoxEx_TextChanged;
    }
    private void Tmr_Tick(object sender, EventArgs e)
    {
        tmr.Stop();
        if (this.StableTextChanged != null)
        {
            this.StableTextChanged(this, new EventArgs());
        }
    }
    private void TextBoxEx_TextChanged(object sender, EventArgs e)
    {
        tmr.Stop();
        tmr.Start();
    }
}