在C#中的文本框中显示秒表当前时间

时间:2018-10-31 21:14:35

标签: c# stopwatch

我在寻找什么:

在加载主窗体时,我希望创建15个秒表,并为每个秒表分配15个textbox.text。这些文本框将显示每个计时器启动后的当前时间:

       public void Form1_Load(object sender, EventArgs e)
    {
        StopWatchCreate();
    }

    public void StopWatchCreate()
    {
        Stopwatch stopwatch1 = new Stopwatch();
        TimerTextBox1.Text = stopwatch1.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch2 = new Stopwatch();
        TimerTextBox2.Text = stopwatch2.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch3 = new Stopwatch();
        TimerTextBox3.Text = stopwatch3.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch4 = new Stopwatch();
        TimerTextBox4.Text = stopwatch4.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch5 = new Stopwatch();
        TimerTextBox5.Text = stopwatch5.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch6 = new Stopwatch();
        TimerTextBox6.Text = stopwatch6.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch7 = new Stopwatch();
        TimerTextBox7.Text = stopwatch7.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch8 = new Stopwatch();
        TimerTextBox8.Text = stopwatch8.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch9 = new Stopwatch();
        TimerTextBox9.Text = stopwatch9.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch10 = new Stopwatch();
        TimerTextBox10.Text = stopwatch10.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch11 = new Stopwatch();
        TimerTextBox11.Text = stopwatch11.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch12 = new Stopwatch();
        TimerTextBox12.Text = stopwatch12.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch13 = new Stopwatch();
        TimerTextBox13.Text = stopwatch13.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch14 = new Stopwatch();
        TimerTextBox14.Text = stopwatch14.Elapsed.ToString("hh\\:mm\\:ss");
        Stopwatch stopwatch15 = new Stopwatch();
        TimerTextBox15.Text = stopwatch15.Elapsed.ToString("hh\\:mm\\:ss");
    }

单击按钮后,我要启动相应的秒表,并让textbox.text显示当前计时器。我遇到的问题在按钮时钟功能下:

public void button1_Click(object sender, EventArgs e)
    {
        Form1.stopwatch1.Start();
    }

但是stopwatch1给出了错误:'TaskTracker.Form1'不包含'stopwatch1'的定义

谢谢您的帮助!

2 个答案:

答案 0 :(得分:2)

也许您似乎想要这个。

只需对这段代码进行一些修改,就可以创建所需的内容。

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp23
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Timer t1;

        Stopwatch s1;
        Stopwatch s2;


        private void Form1_Load(object sender, EventArgs e)
        {
            s1 = new Stopwatch();
            s2 = new Stopwatch();

            t1 = new Timer();
            t1.Interval = 1;
            t1.Tick += T1_Tick;
            t1.Start();
        }

        private void T1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = s1.ElapsedMilliseconds.ToString();
            textBox2.Text = s2.ElapsedMilliseconds.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            s1.Start();
            s2.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            s1.Stop();
            s2.Stop();
        }
    }
}

答案 1 :(得分:1)

首先,正如评论所述,所有秒表变量都必须是Form的字段,而不是现在的局部变量。

这样做:

public partial Form1: Form
{
      Stopwatch stopwatch1 = new Stopwatch();
      Stopwatch stopwatch2 = new Stopwatch();
      // etc

然后要显示秒表的当前值,您将需要一个Timer组件,只需在表单上放置一个Timer

然后为Timer编写一个事件处理程序,如下所示:

private void Timer1_Tick(object sender, EventArgs e)
{
        TimerTextBox1.Text = stopwatch1.Elapsed.ToString("hh\\:mm\\:ss");
        TimerTextBox2.Text = stopwatch2.Elapsed.ToString("hh\\:mm\\:ss");
        // etc
}

Timer应该每秒触发一次(将Interval设置为1000)。

现在,文本框将每秒显示秒表的值。