timer
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.Data.OleDb;using System.Text.RegularExpressions;namespace Ceshi{ public partial class Form1 : Form { float twice; float time = 0 ; private DateTime starttime; //启动时间 private TimeSpan span; //已走时间 private TimeSpan addtime; private DateTime dt; private TimeSpan timenow; //private TimeSpan pausespan;//停止时间 public Form1() { InitializeComponent(); label5.Text = string.Format("00:00:{0}:000", time.ToString("00")); } private void button1_Click(object sender, EventArgs e) { try { timer1.Start(); button1.Enabled = false; button3.Enabled = false; starttime = DateTime.Now; //timer1.Interval = Convert.ToInt32(textBoxTime.Text); timer2.Start(); timer1.Interval = Convert.ToInt32(textBoxTime.Text); timer2.Interval = 10;BaseData(textBoxTime.Text, twice, timenow); } catch { } } private void timer1_Tick(object sender, EventArgs e) { span = (DateTime.Now - starttime).Add(addtime); //string r = Regex.Replace(span.ToString(), @"\.\d $", string.Empty); //span = TimeSpan.Parse(r); //去掉毫秒 //label4.Text = string.Format("{0:d}",span.ToString()); //label4.Text = string.Format("{0}", span.ToString()); twice = Convert.ToInt32(labelTwice.Text); twice ; labelTwice.Text = twice.ToString(); labelTwice.Font = new Font("genericFamily", 15); } private void button2_Click(object sender, EventArgs e) { try { timer1.Stop(); timer2.Stop(); addtime = TimeSpan.Parse(label5.Text);button1.Enabled = true; button3.Enabled = true; BaseData(textBoxTime.Text, twice, timenow); } catch { } } private void Form1_Load(object sender, EventArgs e) { label2.Font = new Font("genericFamily", 15); label3.Font = new Font("genericFamily", 15); labelTwice.Font = new Font("genericFamily", 15); label5.Font = new Font("genericFamily", 15); label4.Font = new Font("genericFamily", 15); label6.Font = new Font("genericFamily", 15); string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" Application.StartupPath @"\sys.mdb"; OleDbConnection conn = new OleDbConnection(str); conn.Open(); string sql = "select top 1 * from jk order by 记录时间 desc"; //降序最大时间获得第一行 //OleDbCommand comm = new OleDbCommand(sql, conn); //OleDbDataReader DataRead = new OleDbDataReader (); //DataRead = comm.ExecuteReader(); //while (DataRead.Read()) //{ // string times = DataRead[0].ToString (); //} //DataRead.Close(); //conn.Close(); DataTable dt=new DataTable(); OleDbDataAdapter DataRead = new OleDbDataAdapter(sql, conn); DataRead.Fill(dt); if (dt.Rows.Count == 0) return; //DataColumn dc = dt.Columns[0]; //行 DataRow DR = dt.Rows[0]; string times1 = DR[0].ToString(); string twice1 = DR[1].ToString(); string time1 = DR[2].ToString(); textBoxTime.Text = times1; labelTwice.Text = twice1; label5.Text = time1; addtime = TimeSpan.Parse(time1); } private void button3_Click(object sender, EventArgs e) { twice = 0; addtime =TimeSpan.Parse(Convert.ToString(0)); timenow =TimeSpan.Parse(Convert.ToString(0)); addtime = TimeSpan.Parse(Convert.ToString(0)); label5.Text = Convert.ToString(timenow addtime); labelTwice.Text = twice.ToString(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { } private void button4_Click(object sender, EventArgs e) { Form2 f = new Form2(); f.Show(); } public void BaseData(string times, float twice, TimeSpan span) { string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" Application.StartupPath @"\sys.mdb"; OleDbConnection conn = new OleDbConnection(str); conn.Open(); string sql = "INSERT INTO jk(间隔时间,次数,总时间,记录时间)values('" times "','" twice "','" span "','" DateTime.Now "' )"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); conn.Close(); } private void timer2_Tick(object sender, EventArgs e) { timenow = (DateTime.Now - starttime).Add(addtime); label5.Text = string.Format("{0:d}", timenow.ToString()); dt = DateTime.Now; label6.Text = dt.ToString(); } }}
评论