Jump to content

Simple Stop Watch Application

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Ok, so i'm trying to get back in to the swing of C# but i'm running into a constant problem when creating these simple applications. I can't get a label to dynamically update in a form application. In command prompt it's not a big deal but when I try to do it in forms it just will not work.

Here's what i'm currently trying to do: Create a stop watch in which you can see the time as it counts, dynamically. Here's my code:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Threading;


namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        Stopwatch stopWatch = new Stopwatch();


        public Form1()

        {

            InitializeComponent();

        }


        private void start_Click(object sender, EventArgs e)

        {

            stopWatch.Start();

        }


        private void stop_Click(object sender, EventArgs e)

        {

            stopWatch.Stop();

            timePrint();

        }


        private void reset_Click(object sender, EventArgs e)

        {

            stopWatch = Stopwatch.StartNew();

        }


        private void timePrint()

        {

            label1.Text = stopWatch.ElapsedTicks.ToString();

            hours.Text = "Time elapsed: " + stopWatch.Elapsed;

        }

    }

}


Any suggestions on how I should go about this?

#2
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Drag and drop a timer into your form. Add to your constructor :
timer1.Start();

Make a Tick event, call timePrint() in it, it should work.

#3
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I tried that with no luck. I thought the timer control was only for ASP.NET?

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
If it appears in the controls list in the IDE, it can be used in a form.

When you start your stopwatch you'll also need to start the timer.

#5
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
    public partial class Form1 : Form {

        Stopwatch stopWatch = new Stopwatch();


        public Form1() {

            InitializeComponent();

            stopWatch.Start();

            timer1.Start();

        }


        private void start_Click(object sender, EventArgs e) {

            stopWatch.Start();

        }


        private void stop_Click(object sender, EventArgs e) {

            stopWatch.Stop();

        }


        private void timer1_Tick(object sender, EventArgs e) {

            label1.Text = stopWatch.ElapsedTicks.ToString();

            hours.Text = "Time elapsed: " + stopWatch.Elapsed;

        }


        private void reset_Click(object sender, EventArgs e) {

            stopWatch = Stopwatch.StartNew();

        }

    }


I tried the timer and it works for me

#6
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I copied the exact code that you put in and added the timer and label 1. It doesn't print out anything though or show any time when I start or stop. This is the code I have now


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Threading;


namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        Stopwatch stopWatch = new Stopwatch();


        public Form1()

        {

            InitializeComponent();

            stopWatch.Start();

            timer1.Start();

        }


        private void start_Click(object sender, EventArgs e)

        {

            stopWatch.Start();

        }


        private void stop_Click(object sender, EventArgs e)

        {

            stopWatch.Stop();

        }


        private void timer1_Tick(object sender, EventArgs e)

        {

            label1.Text = stopWatch.ElapsedTicks.ToString();

            hours.Text = "Time elapsed: " + stopWatch.Elapsed;

        }


        private void reset_Click(object sender, EventArgs e)

        {

            stopWatch = Stopwatch.StartNew();

        }

    }

}


Posted Image

#7
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Did you attach the timer1_Tick event to the Tick event of the timer?

#8
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Yes, you need to attach it to the Timer_Tick event.

#9
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
:D It works. And I can use Elapsed.Minutes/Elapsed.Hours to style it how I want it. That's great thanks guys




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users