0

I got an application which sends Jobs to an AIV. Now I got a TextBox which gets it input from a barcode scanner. I got the following custom TextBox class (got it from: C# wait for user to finish typing in a Text Box). The method HandleDelayedTextChangedTimerTick fires once and works fine and prints the "Hello world" after 10 seconds after input. I also got the following form: where I connect to the robot and the jobs are queued in the Form.CS.

My question is: How do I use the HandleDelayedTextChangedTimerTick in the Form.CS to fire direct a job to the robot after the input and the 10 seconds delay?

enter image description here

 public class MyTextBox : TextBox
{
    private TextBox textBox1;
    private Timer m_delayedTextChangedTimer;

    public event EventHandler DelayedTextChanged;

    public MyTextBox() : base()
    {
        this.DelayedTextChangedTimeout = 10 * 1000; // 10 seconds
    }

    protected override void Dispose(bool disposing)
    {
        if (m_delayedTextChangedTimer != null)
        {
            m_delayedTextChangedTimer.Stop();
            if (disposing)
                m_delayedTextChangedTimer.Dispose();
        }

        base.Dispose(disposing);
    }

    public int DelayedTextChangedTimeout { get; set; }

    protected virtual void OnDelayedTextChanged(EventArgs e)
    {
        if (this.DelayedTextChanged != null)
            this.DelayedTextChanged(this, e);
    }

    protected override void OnTextChanged(EventArgs e)
    {
        this.InitializeDelayedTextChangedEvent();
        base.OnTextChanged(e);
    }

    private void InitializeDelayedTextChangedEvent()
    {
        if (m_delayedTextChangedTimer != null)
            m_delayedTextChangedTimer.Stop();

        if (m_delayedTextChangedTimer == null || m_delayedTextChangedTimer.Interval != this.DelayedTextChangedTimeout)
        {
            m_delayedTextChangedTimer = new Timer();
            m_delayedTextChangedTimer.Tick += new EventHandler(HandleDelayedTextChangedTimerTick);
            m_delayedTextChangedTimer.Interval = this.DelayedTextChangedTimeout;
        }

        m_delayedTextChangedTimer.Start();
    }

    private void HandleDelayedTextChangedTimerTick(object sender, EventArgs e)
    {
        Timer timer = sender as Timer;
        timer.Stop();
        Console.WriteLine("Hello world");
        this.OnDelayedTextChanged(EventArgs.Empty);
    }

    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(0, 0);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 22);
        this.textBox1.TabIndex = 0;
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        this.ResumeLayout(false);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
tijn167
  • 547
  • 2
  • 21
  • Are you getting any error? If yes then can you please also mention that? – Keyur Ramoliya Jan 09 '19 at 08:42
  • No errors. and everything works fine with the Hello World. But I need to use the function is the Form.CS – tijn167 Jan 09 '19 at 08:46
  • You are firing an event. So you need to register that event in Form.cs. Something like "myTextBox.DelayedTextChanged += HandleMyTBDelayedTextChanged;" where `HandleMyTBDelayedTextChanged` is your EventHandling Method in Form.cs – Fildor Jan 09 '19 at 08:53
  • BTW: I am not sure if this is the best solution when using WPF. I'd have expected the MVVM pattern in use, where a ViewModel would take care of that. – Fildor Jan 09 '19 at 08:56
  • Rather than a Timer you could just make a method you call on textchanged async and use await.Task.Delay(10000). Then compare a timestamp to see if there was user input in that time. What method do you wish to run? Because substituting whatever that is for your console.writeline seems like it'd be the answer. – Andy Jan 09 '19 at 11:38

0 Answers0