如何使用C#制作秒表

时间:2016-09-17 08:06:39

标签: c# winforms

我只是想问,如果我能用C#制作秒表,我试过:

        private void button2_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        int st = 00;
        int m = 00;

        string stime = "00:00";
        if(st == 60)
        {
            m++;
            st = 00;
        }
        else
        {
            st++;
        }
        if (m == 60)
        {
            m = 00;
        }
        if(st < 10)
        {
            st = 0 + st;
        }
        if(m < 10)
        {
            m = 0 + m;
        }
        stime = m.ToString() + ":" + st.ToString();
        label3.Text = stime;
    }

这但它没有奏效。我的计时器已设置,计时器的间隔为1000毫秒。有人能帮助我吗?

1 个答案:

答案 0 :(得分:4)

在我看来,你更喜欢制作手表而不是秒表?

如果您正在制作秒表,我认为您需要一个包含开始时间的课程/属性:

// in is the inputstream that you got from the Uri
// dst is a file to your app internal cache
public void copy(Uri uri, File dst) throws IOException {
    InputStream in = contentResolver.openInputStream(uri);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

然后在timer1_Tick中你可以这样做:

private DateTime _start;
private void button2_Click(object sender, EventArgs e)
{
    _start = DateTime.Now;
    timer1.Start();
}

似乎你在timer1_Tick中的当前代码只有局部变量,因此总会生成相同的时间? : - )