Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the time interval between typing characters in a TextBox, in C#

Tags:

c#

I have a form that has a TextBox and a Label and I want to get the time of the first character entered in the textbox. Then if the user enters more than ten charcaters, the time between the first charcter entered and the tenth charcter entered is displayed in the label.

Can any one help me please? I'm using C#

Here is the code but I cannot complete it and I have many things that need to be written but I don't know how to continue.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace n
{
    public partial class Form1 : Form
    {
        int count=0;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
                {
            DateTime t1 = new DateTime();// the time when entering the first charcter
            DateTime t2 = new DateTime();
            t2 = System.DateTime.Now - t1;
            int index = textBox1.SelectionStart;
            Point p;
            p = textBox1.GetPositionFromCharIndex(index);
            Thread t = new Thread(counttext);
            t.Start();
            label1.Text = "t2";


        }

        private int counttext()
        {
            while (textBox1.Text.Length < 10)
            {
                count++;
                if (count == 10)
                    return count;
            }
        }






    }
}
like image 931
sama Avatar asked Jan 01 '26 04:01

sama


1 Answers

this is simple, you have to track the change event of text box and maintain count, start the timer when count is 0 and stop the timer and display the ticks.

Please note that I assumed you are using windows application, text change event is inbuilt to c#, just double click the text box, time is the default control as well.

like image 107
Prashant Lakhlani Avatar answered Jan 02 '26 18:01

Prashant Lakhlani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!