4 Replies - 33236 Views - Last Post: 17 December 2008 - 09:05 AM Rate Topic: -----

#1 learningc#   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-December 08

how can i calculate the time remaining using a progress bar

Post icon  Posted 17 December 2008 - 05:36 AM

hi ppl! im creating a maths game in c#. im stuggling in trying to calculate the time remaining on a progress bar...im also using a timer.
i'l paste my code for the timer and progress bar below..any hints, suggestions, tips or solutions would be really really appreciated!! thanks in advance!

progress bar:
 for (value = 0; value != limit; value++)  
			{
				QuestionTimer.Enabled = true;   //timer "wholeGame" is enabled 
				pgbar.Maximum = limit;
				pgbar2.Maximum = limit;
				QuestionTimer.Enabled = true;
				GameTimer.Enabled = true;

			}

timer:
  private void QuestionTimer_Tick(object sender, EventArgs e)
		{

			if (pgbar.Value < pgbar.Maximum)
			{
				pgbar.Value++;

			}
			else
			{

				redoRandom();

			}

			if (pgbar.Value >= 20)
			{
				lblreportime.Text = " You Have 10 seconds left!";

			}

			else
			{
				lblreportime.Text = "";
			}

			
		}


This post has been edited by learningc#: 17 December 2008 - 06:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how can i calculate the time remaining using a progress bar

#2 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: how can i calculate the time remaining using a progress bar

Posted 17 December 2008 - 06:08 AM

Hi there,

Wouldn't it be better to have the timer ticking whilst it remains under the max allowed time, and calculating the percentage of the progress bar:

percentagebar.value = (100/maxtime) * elapsedseconds

And just using the progress bar as a display of a percentage of the elapsed time?
Was This Post Helpful? 0
  • +
  • -

#3 learningc#   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-December 08

Re: how can i calculate the time remaining using a progress bar

Posted 17 December 2008 - 06:12 AM

hey! thanks for the reply!!
i need to calculate the timeremaining because i want to add bonus points for every second rememaing...for e.g. if the user is playing the easy level and has 10 seconds remaining after answering the question correctly, i want to multiply 2 * ( 10 seconds) = bonus points. for medium - 3 * ( secondsremaining) and for hard 4* ( seconds remaining). but im stuck on trying to calculate the seconds remaining.

View Postragingben, on 17 Dec, 2008 - 05:08 AM, said:

Hi there,

Wouldn't it be better to have the timer ticking whilst it remains under the max allowed time, and calculating the percentage of the progress bar:

percentagebar.value = (100/maxtime) * elapsedseconds

And just using the progress bar as a display of a percentage of the elapsed time?

This post has been edited by learningc#: 17 December 2008 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: how can i calculate the time remaining using a progress bar

Posted 17 December 2008 - 06:57 AM

Ok I see... try this:

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

namespace Timer_thing
{
	public partial class TestProgram : Form
	{
		private Int32 maxTime = 30;
		private Int32 elapsedTime;

		public TestProgram()
		{
			InitializeComponent();
		}

		private void startButton_Click(object sender, EventArgs e)
		{
			// reset stuff
			elapsedTimeProgressBar.Value = 0;
			elapsedTime = 0;
			// start timer
			counntdownTimer.Start();
		}

		private void counntdownTimer_Tick(object sender, EventArgs e)
		{
			// if time is less than max time
			if (elapsedTime < maxTime)
			{
				// increment elapsed time
				elapsedTime++;
				// display progress
				elapsedTimeProgressBar.Value = (100 / maxTime) * elapsedTime;
			}
		}

		private void stopButton_Click(object sender, EventArgs e)
		{
			// stop timer
			counntdownTimer.Stop();
			// display results
			MessageBox.Show(String.Format("You took {0} seconds giving you a score of {1} (4 * {2})",elapsedTime, (maxTime - elapsedTime) * 4,maxTime - elapsedTime ),"Well done!");
		}
	}
}



or try the zipped up atachment for the solution.

Hope this helps!

:)

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 learningc#   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-December 08

Re: how can i calculate the time remaining using a progress bar

Posted 17 December 2008 - 09:05 AM

hey there! THHANKS A LOT!! :D :^:

View Postragingben, on 17 Dec, 2008 - 05:57 AM, said:

Ok I see... try this:

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

namespace Timer_thing
{
	public partial class TestProgram : Form
	{
		private Int32 maxTime = 30;
		private Int32 elapsedTime;

		public TestProgram()
		{
			InitializeComponent();
		}

		private void startButton_Click(object sender, EventArgs e)
		{
			// reset stuff
			elapsedTimeProgressBar.Value = 0;
			elapsedTime = 0;
			// start timer
			counntdownTimer.Start();
		}

		private void counntdownTimer_Tick(object sender, EventArgs e)
		{
			// if time is less than max time
			if (elapsedTime < maxTime)
			{
				// increment elapsed time
				elapsedTime++;
				// display progress
				elapsedTimeProgressBar.Value = (100 / maxTime) * elapsedTime;
			}
		}

		private void stopButton_Click(object sender, EventArgs e)
		{
			// stop timer
			counntdownTimer.Stop();
			// display results
			MessageBox.Show(String.Format("You took {0} seconds giving you a score of {1} (4 * {2})",elapsedTime, (maxTime - elapsedTime) * 4,maxTime - elapsedTime ),"Well done!");
		}
	}
}



or try the zipped up atachment for the solution.

Hope this helps!

:)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1