开发者

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

开发者 https://www.devze.com 2022-12-24 03:35 出处:网络
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 charc

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;
            }
        }






    }
}


google c# tutorials.... and my i suggest start with some basic console programs so you know how the basics work then go onto winforms....


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.


I agree with Petoj, you really have to go over the basics of C# and the .NET Framework. But here's a little bit of code to help you:

public partial class Form1 : Form 
{ 
   DateTime TimeTypingBegan { get; set; }

   public Form1() 
   { 
      InitializeComponent(); 
   }

   private void textBox1_TextChanged(object sender, EventArgs e) 
   {
      if (textBox1.Text.Length == 1)
         TimeTypingBegan = DateTime.Now;
      else if (textBox1.Text.Length == 10)
         label1.Text = (DateTime.Now - TimeTypingBegan).ToString();
   } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消