# how can i calculate the time remaining using a progress bar

Page 1 of 1

## 4 Replies - 33236 Views - Last Post: 17 December 2008 - 09:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=77178&amp;s=51a8e54c19ec4dd1f3c11d7bf219d795&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 learningc#

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

# how can i calculate the time remaining using a progress bar

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

Reputation: 177
• 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?

### #3 learningc#

Reputation: 1
• 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

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.

ragingben, 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

### #4 ragingben

Reputation: 177
• 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!

### #5 learningc#

Reputation: 1
• 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!!

ragingben, 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!