C
C#2y ago
The Joker

❔ Help C# countdown timer

I'm creating a countdown timer, I managed to make the minutes and seconds work, however I need it to have hours too but I'm not managing to set the code logic to show the hours, in my case the user type in a textbox the time and click on the button time starts running until it reaches 0, please could someone help me
int time = 0;
int minutes = 0;
int seconds = 0;
int hours = 0;

private void btnStart_Click(object sender, EventArgs e)
{
time = Convert.ToInt32(txtTempo.Text);

if(time >= 60)
{
hours = time / 24;
minutes = time % 60;
seconds = time % 60;
}
else
{
hours = 0;
minutes = 0;
seconds = time;

}
lblTempo.Text = string.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, '0'), minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
seconds--;

if (hours > 0)
{
if (minutes < 0)
{
minutes = 59;
hours--;

if (minutes > 0)
{
if (seconds < 0)
{
seconds = 59;
minutes--;

}
}
}
}
lblTempo.Text = string.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, '0'), minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));

if (hours == 0 && minutes == 0 && seconds == 0)
{
timer1.Enabled = false;
}
}
int time = 0;
int minutes = 0;
int seconds = 0;
int hours = 0;

private void btnStart_Click(object sender, EventArgs e)
{
time = Convert.ToInt32(txtTempo.Text);

if(time >= 60)
{
hours = time / 24;
minutes = time % 60;
seconds = time % 60;
}
else
{
hours = 0;
minutes = 0;
seconds = time;

}
lblTempo.Text = string.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, '0'), minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
seconds--;

if (hours > 0)
{
if (minutes < 0)
{
minutes = 59;
hours--;

if (minutes > 0)
{
if (seconds < 0)
{
seconds = 59;
minutes--;

}
}
}
}
lblTempo.Text = string.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, '0'), minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));

if (hours == 0 && minutes == 0 && seconds == 0)
{
timer1.Enabled = false;
}
}
30 Replies
phaseshift
phaseshift2y ago
Hours= time/24 (your code) What units does time have here?
The Joker
The JokerOP2y ago
Hours, Minutes and Seconds the user will type in the text box the time in seconds
phaseshift
phaseshift2y ago
Tell me, what is seconds/24? Or what are you trying to calculate with that?
The Joker
The JokerOP2y ago
precisely this part that I don't understand, I managed to get minutes and seconds to work but the hours I couldn't
phaseshift
phaseshift2y ago
Well how many seconds do you need to display 1hr ?
The Joker
The JokerOP2y ago
3600
phaseshift
phaseshift2y ago
So where did you pull '24' from?
The Joker
The JokerOP2y ago
I don't know I'm still a beginner
phaseshift
phaseshift2y ago
Beginner at multiplication? You have a lot of 'if' without else. Looks very complicated But it seems like the first thing you do is take away 1 second, correct? So tell me in words, what is the order of checks you need to do for changing seconds, minutes, hours Eg starting from 1h01m01s
The Joker
The JokerOP2y ago
the order is the user types in the text box a value that is counted in seconds, when I click on start automatically the label presents me with the value converted into hh:mm:ss, minutes and seconds I managed to make it work, but when the user types for example 3700 seconds it does not count the time. that is, it does not appear this what i'm doing is a countdown timer
phaseshift
phaseshift2y ago
I don't think your seconds/minutes works anyway. But I'm not talking about that order I'm asking about the order of checks you need to make in the code The algorithm.
The Joker
The JokerOP2y ago
The Joker
The JokerOP2y ago
minutes and seconds work my problem is with values ​​above 3600 seconds
The Joker
The JokerOP2y ago
The Joker
The JokerOP2y ago
3800 sec
phaseshift
phaseshift2y ago
That's broken at 0 minutes 59s Right?
The Joker
The JokerOP2y ago
the hours do not appear, it only increases in minutes yes seconds to 0 return for 59
phaseshift
phaseshift2y ago
Oh ok, I see it . Well, you know how when you're watching a digital countdown, the smallest unit moves first, then slowly it works to the left/larger things That's the order your checks should be. You have them backwards And since you're trying to get things working backwards, you've got an impossible combination of ifs
The Joker
The JokerOP2y ago
and how would it be correct?
phaseshift
phaseshift2y ago
Check seconds first
phaseshift
phaseshift2y ago
phaseshift
phaseshift2y ago
If minutes is LT 0 it can never be GT 0. that inner block will never be hit
The Joker
The JokerOP2y ago
ok thanks wow very complicated
phaseshift
phaseshift2y ago
If you keep going with hours > minutes > seconds, then yes. Hence why I suggest you reverse your order Or the alternative is just redo the initial calculation with 1 fewer seconds
The Joker
The JokerOP2y ago
can you give me an example
phaseshift
phaseshift2y ago
Of what
The Joker
The JokerOP2y ago
Can someone please help me?
Anton
Anton2y ago
use DateTime there's DateTime.FromSeconds it does all of that for you and you could also do dt -= DateTime.FromSeconds(1) if you use that
The Joker
The JokerOP2y ago
Solved close this topic.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server