0

What I am looking for:

When my main form loads, I want 15 stopwatches created and 15 textbox.text's to be assigned to each stopwatch. These textboxes will display the current time of each timer after it is started:

       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");
    }

After a button click I want the corresponding stopwatch to start and the textbox.text to display the current timer. The issue I am running into is under the button clock function:

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

but stopwatch1 is giving the error: 'TaskTracker.Form1' does not contain a definition for 'stopwatch1'

Thank you for your help with this!

Shannon C
  • 31
  • 3

2 Answers2

2

Perhaps you seem to want this.

With a little modification in this code, you can create what you want.

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();
        }
    }
}
Mr.feel
  • 315
  • 1
  • 3
  • 12
1

First, as the comments says, all the Stopwatch variables must be fields of the Form, not local variables, as they are now.

Do like this:

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

Then to display the current value of the StopWatches, you will need a Timer Component, simply drop a Timer on the Form.

Then write an eventhandler for the Timer, that looks like this:

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
}

The Timer should propably trigger once per second (set Interval to 1000).

Now the textboxes will show the value of the Stopwatches every second.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57