1

我有计时器使用 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);

    }

}

}

4

2 回答 2

0

计时器用于显示经过的时间……您为什么希望它做其他事情?

来自MSDN

Public method   Timer() 

初始化 Timer 类的新实例。

Public method   Timer(IContainer)   

与指定的容器一起初始化 Timer 类的新实例。

所以不行 :)

于 2013-11-13T07:00:52.407 回答
0

秒表是用来测量时间的,所以你不能这样做。

您可以在其上创建一个包装器并执行类似的操作。

public class MyStopWatch
{
    private TimeSpan offset;
    private Stopwatch stopwatch = new Stopwatch();
    public MyStopWatch(TimeSpan offset)
    {
        this.offset = offset;
    }

    public TimeSpan Elapsed { get { return stopwatch.Elapsed + offset; } }
    public bool IsRunning { get { return stopwatch.IsRunning; } }

    public void Reset()
    {
        stopwatch.Reset();
    }
    public void Restart()
    {
        stopwatch.Restart();
    }
    public void Start()
    {
        stopwatch.Start();
    }
    public void Stop()
    {
        stopwatch.Stop();
    }
}

用例

var mySw =new MyStopWatch(TimeSpan.FromSeconds(15));
mySw.Start();
mySw.Elapsed//00:00:15
于 2013-11-13T07:55:21.587 回答