Trying to create stopwatches that will display in textboxes but I am getting a " does not contain a definition" error.

Hello!

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

Open in new window


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

Open in new window

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

Thank you for your help with this!
LVL 2
ShannonCallahanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sam JacobsDirector of Technology Development, IPMCommented:
Put the initialization of the stopwatches inside the class, before the constructor:
    public partial class Form1 : Form
    {
        Stopwatch stopwatch1 = new Stopwatch();
        Stopwatch stopwatch2 = new Stopwatch();
        etc ...

        public Form1()
        {
            InitializeComponent();

        }
etc...

Open in new window


Then remove the Form1. when you start the stopwatches.
 
       private void button1_Click(object sender, EventArgs e)
        {
            stopwatch1.Start();
            stopwatch2.Start();
            etc.
        }

Open in new window

Chris StanyonWebDevCommented:
Your stopwatches are not properties on the form - they're local variables that are declared within your StopWatchCreate function, so they're only available in that function.

You need to declare them at the class level (the form level in your case). Something like so;

Class Form1
{
private Stopwatch stopwatch1;
private Stopwatch stopwatch2;
private Stopwatch stopwatch3;

    public void StopWatchCreate() 
    {
        TimerTextBox1.Text = stopwatch1.Elapsed.ToString("hh\\:mm\\:ss");
        ...
    }        
}

Open in new window

ShannonCallahanAuthor Commented:
OK. I added the Stopwatch at the class level:

public partial class Form1 : Form
    {
        Stopwatch stopwatch1 = new Stopwatch();

Open in new window


And I have the textbox text set on load of Form1:

        
public void Form1_Load(object sender, EventArgs e)
        {
            TimerTextBox1.Text = stopwatch1.Elapsed.ToString("hh\\:mm\\:ss");
        }

Open in new window


And my start button function:

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

Open in new window


The program will run, but the TimerTextBox1.text does not change?
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Chris StanyonWebDevCommented:
OK. You're trying to set the textbox once only - when the form first loads.

You won't have started the stopwatch by this time, and even if you had, it won't constantly change the textbox as that only happens when you first load the form.

If you're wanting to constantly update the textbox, then you're going to need to thread the application somehow, and constantly poll the timer for updates. If you don't do this on a thread (background worker etc), then your UI will just lock.

The general approach you probably need is this:

declare the stopwatch at the class level.
start the stopwatch on form load
constantly poll the stopwatch and update the textbox from a background thread.
Sam JacobsDirector of Technology Development, IPMCommented:
You can:
- place a Timer control on the form,
- set the Interval to 1000 (ms.), and
- set Enabled to True.

Then move the stopwatch elapsed code to the timer Tick event:
        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...
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShannonCallahanAuthor Commented:
Thank you both, Sam and Chris, for your time and assistance!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.