5 Replies - 599 Views - Last Post: 23 July 2013 - 11:56 PM Rate Topic: -----

#1 Crovox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-April 13

Stopwatch class/accumulating seconds

Posted 23 July 2013 - 08:12 PM

This application calculates a specific price every second and it displays it to the user (the price is based off of whatever hourly charge the user enters). I'm having an issue where my price gets set back to $0.00 whenever my timer hits one minute. I realize this is because when it reaches 59 seconds it is resetting to 0 seconds, but I'm having trouble remedying the situation. Any suggestions?

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

        Fare fare = new Fare();

        private void btnStartStop_Click(object sender, EventArgs e)
        {

            if (btnStartStop.Text == "Start")
            {
                timer1.Enabled = true;
                fare.StartTimer();
                btnStartStop.Text = "Stop";
            }
            else
            {
                timer1.Enabled = false;
                fare.StopTimer();
                btnStartStop.Text = "Start";
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (btnStartStop.Text == "Start")
            {
                lblFee.Text = "$0.00";
                lblClock.Text = "00:00:00";
                fare.ResetTimer();
            }
            else
            {
                lblFee.Text = "$0.00";
                fare.ResetTimer();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblClock.Text = fare.SetClock();

            try
            {
                double price = Convert.ToDouble(txtPrice.Text);
                lblFee.Text = Convert.ToString(fare.SetCost(price).ToString("c"));
            }
            catch
            {
            }
        }
    }


Fare Class
class Fare
    {
        private Stopwatch timer = new Stopwatch();

        public void StartTimer()
        {
            timer.Start();
        }

        public void StopTimer()
        {
            timer.Stop();
        }

        public void ResetTimer()
        {
            timer.Restart();
        }

        public string SetClock()
        {
            string clock;
            int hours = timer.Elapsed.Hours, minutes = timer.Elapsed.Minutes, seconds = timer.Elapsed.Seconds;

            if (hours < 10)
            {
                clock = "0" + hours + ":";
            }
            else
            {
                clock = hours + ":";
            }

            if (minutes < 10)
            {
                clock += "0" + minutes + ":";
            }
            else
            {
                clock += minutes + ":";
            }

            if (seconds < 10)
            {
                clock += "0" + seconds;
            }
            else
            {
                clock += seconds;
            }

            return clock;
        }

        public double SetCost(double price)
        {
            double totalPrice;
            int totalSeconds = 0;
            double costPerSecond = price / 3600;
            totalSeconds += timer.Elapsed.Seconds;
            totalPrice = costPerSecond * totalSeconds;
            return totalPrice;
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Stopwatch class/accumulating seconds

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: Stopwatch class/accumulating seconds

Posted 23 July 2013 - 08:20 PM

Why not just use the TotalSeconds property of the TimeSpan?
http://msdn.microsof...talseconds.aspx
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: Stopwatch class/accumulating seconds

Posted 23 July 2013 - 08:25 PM

Also, you are doing things The Hard Way™ with your SetClock() method. Just use TimeSpan.ToString() or one of it's variants:
http://msdn.microsof...y/1ecy8h51.aspx
Was This Post Helpful? 0
  • +
  • -

#4 Crovox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-April 13

Re: Stopwatch class/accumulating seconds

Posted 23 July 2013 - 08:45 PM

Are you saying not to use the Stopwatch class at all?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Stopwatch class/accumulating seconds

Posted 23 July 2013 - 09:42 PM

This is just school homework, right? This isn't real code being used out in the real world, or for a contract job, is it?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: Stopwatch class/accumulating seconds

Posted 23 July 2013 - 11:56 PM

I'm saying that Stopwatch.Elapsed is of type TimeSpan and that you should have taken time to read the documentation instead of relying on Intellisense.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1