我有计时器使用 System.Diagnostics 在 datagridview 中运行的代码,但是时间从 00:00:00 开始,我想要一个指定的时间启动计时器。可能吗??如何??请帮助.....我只是 c# 的新手,我真的很困惑。请帮忙...
谢谢。
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.Diagnostics;
namespace TryRoadrunner
{
public partial class Form1 : Form
{
System.Windows.Forms.Timer timer1 = new Timer();
DataTable dtStopwatches = new DataTable();
Dictionary<int, Stopwatch> swDct = new Dictionary<int, Stopwatch>();
Dictionary<int, Stopwatch> wDct = new Dictionary<int, Stopwatch>();
int nextID = 0;
int nextID1 = 0;
public Form1()
{
InitializeComponent();
// for data source
dtStopwatches.Columns.Add("Ticket", typeof(string));
dtStopwatches.Columns.Add("Customer Name", typeof(string));
dtStopwatches.Columns.Add("Area", typeof(string));
dtStopwatches.Columns.Add("Instller", typeof(string));
dtStopwatches.Columns.Add("Endorsed Date", typeof(string));
dtStopwatches.Columns.Add("ID", typeof(int));
dtStopwatches.Columns.Add("ID1", typeof(int));
dtStopwatches.Columns.Add("BBX", typeof(string));
dtStopwatches.Columns.Add("BBZ / BBC", typeof(string));
dataGridView1.DataSource = dtStopwatches;
// create 4 buttons everytime another row is added.
foreach (string buttonName in new string[] { "Start BBX", "Pause BBX", "StartBBZ", "Pause BBZ" })
{
DataGridViewButtonColumn colTemp = new DataGridViewButtonColumn();
colTemp.Name = buttonName + "Col";
colTemp.HeaderText = buttonName;
colTemp.Width = 50;
dataGridView1.Columns.Add(colTemp);
}
timer1.Tick += (timer1_Tick);
timer1.Interval = 50;
timer1.Start();
timer2.Tick += (timer2_Tick);
timer2.Interval = 50;
timer2.Start();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
Stopwatch swTemp = swDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID1"].Value];
switch (dataGridView1.Columns[e.ColumnIndex].HeaderText)
{
case "Start BBX":
swTemp.Start();
break;
case "Pause BBS":
swTemp.Stop();
break;
}
}
if (e.RowIndex > -1)
{
Stopwatch Temp = wDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value];
switch (dataGridView1.Columns[e.ColumnIndex].HeaderText)
{
case "Start BBZ":
Temp.Start();
break;
case "Pause BBZ":
Temp.Stop();
break;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
foreach (DataRow dRow in dtStopwatches.Rows)
{
string elapsedString = swDct[(int)dRow["ID1"]].Elapsed.ToString(@"hh\:mm\:ss");
dRow["BBX"] = elapsedString;
}
timer1.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
foreach (DataRow dRow in dtStopwatches.Rows)
{
string elapsed = wDct[(int)dRow["ID"]].Elapsed.ToString(@"hh\:mm\:ss");
dRow["BBZ / BBC"] = elapsed;
}
timer2.Start();
}
private void btnAddStopwatch_Click(object sender, EventArgs e)
{
dtStopwatches.Rows.Add(txt_tn.Text, txt_cn.Text, txt_area.Text, txt_installer.Text, txt_date.Text, nextID, nextID1, "00:00:00");
swDct.Add(nextID, new Stopwatch());
wDct.Add(nextID, new Stopwatch());
nextID++;
nextID1++;
txt_area.Text = "";
txt_cn.Text = "";
txt_date.Text = "";
txt_installer.Text = "";
txt_tn.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
//================
dataGridView1.Columns["ID"].Visible = false;
dataGridView1.Columns["ID1"].Visible = false;
}
private void btn_delete_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
}
}