How can i make a countdown timer in C# that accounts for hours, minutes and seconds? [duplicate]
This question already has an answer here:
How can I String.Format a TimeSpan object with a custom format in .NET?
19 answers
I am not a good programmer, it's rather my hobby.
So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.
int i = 000000;
private void timer1_Tick(object sender, EventArgs e)
{
{
i++;
textBox1.Text = i.ToString() + "00:00:00";
}
}
When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
Thank you for reading my post and again, sorry for having soo bad programming knowledge.
c#
marked as duplicate by mjwills, Community♦ Jan 21 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I String.Format a TimeSpan object with a custom format in .NET?
19 answers
I am not a good programmer, it's rather my hobby.
So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.
int i = 000000;
private void timer1_Tick(object sender, EventArgs e)
{
{
i++;
textBox1.Text = i.ToString() + "00:00:00";
}
}
When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
Thank you for reading my post and again, sorry for having soo bad programming knowledge.
c#
marked as duplicate by mjwills, Community♦ Jan 21 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I String.Format a TimeSpan object with a custom format in .NET?
19 answers
I am not a good programmer, it's rather my hobby.
So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.
int i = 000000;
private void timer1_Tick(object sender, EventArgs e)
{
{
i++;
textBox1.Text = i.ToString() + "00:00:00";
}
}
When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
Thank you for reading my post and again, sorry for having soo bad programming knowledge.
c#
This question already has an answer here:
How can I String.Format a TimeSpan object with a custom format in .NET?
19 answers
I am not a good programmer, it's rather my hobby.
So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.
int i = 000000;
private void timer1_Tick(object sender, EventArgs e)
{
{
i++;
textBox1.Text = i.ToString() + "00:00:00";
}
}
When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
Thank you for reading my post and again, sorry for having soo bad programming knowledge.
This question already has an answer here:
How can I String.Format a TimeSpan object with a custom format in .NET?
19 answers
c#
c#
edited Jan 19 at 9:36
Prashant Pimpale
3,2573830
3,2573830
asked Jan 19 at 9:31
Jaan SuurJaan Suur
45
45
marked as duplicate by mjwills, Community♦ Jan 21 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by mjwills, Community♦ Jan 21 at 16:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use this:
TimeSpan time = TimeSpan.FromSeconds(i);
textBox1.Text = time.ToString(@"hh:mm:ss");
add a comment |
Ok, so you know how to use Timer
but your problem is with string
, when you use +
on string
you are concatenating
them, so say "hello" + " world"
is equal to "hello world"
, so when you concat i
with 00:00:00
the output you see is very logical.
you can use snippet below to achieve your goal(just replace your form class content with this)
private Timer _timer;
private Label _label;
private int _elapsedSeconds;
public Form1()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
_label.Text = time.ToString(@"hh:mm:ss");
};
_label = new Label();
Controls.Add(_label);
}
Edit
Tnx to @Juliet Wilson I edited how time is converted to string
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not useToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?
– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
add a comment |
I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),StopWatch
doesn't have any count-down ability at all.
– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use this:
TimeSpan time = TimeSpan.FromSeconds(i);
textBox1.Text = time.ToString(@"hh:mm:ss");
add a comment |
You can use this:
TimeSpan time = TimeSpan.FromSeconds(i);
textBox1.Text = time.ToString(@"hh:mm:ss");
add a comment |
You can use this:
TimeSpan time = TimeSpan.FromSeconds(i);
textBox1.Text = time.ToString(@"hh:mm:ss");
You can use this:
TimeSpan time = TimeSpan.FromSeconds(i);
textBox1.Text = time.ToString(@"hh:mm:ss");
answered Jan 19 at 9:57
Juliet WilsonJuliet Wilson
561
561
add a comment |
add a comment |
Ok, so you know how to use Timer
but your problem is with string
, when you use +
on string
you are concatenating
them, so say "hello" + " world"
is equal to "hello world"
, so when you concat i
with 00:00:00
the output you see is very logical.
you can use snippet below to achieve your goal(just replace your form class content with this)
private Timer _timer;
private Label _label;
private int _elapsedSeconds;
public Form1()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
_label.Text = time.ToString(@"hh:mm:ss");
};
_label = new Label();
Controls.Add(_label);
}
Edit
Tnx to @Juliet Wilson I edited how time is converted to string
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not useToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?
– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
add a comment |
Ok, so you know how to use Timer
but your problem is with string
, when you use +
on string
you are concatenating
them, so say "hello" + " world"
is equal to "hello world"
, so when you concat i
with 00:00:00
the output you see is very logical.
you can use snippet below to achieve your goal(just replace your form class content with this)
private Timer _timer;
private Label _label;
private int _elapsedSeconds;
public Form1()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
_label.Text = time.ToString(@"hh:mm:ss");
};
_label = new Label();
Controls.Add(_label);
}
Edit
Tnx to @Juliet Wilson I edited how time is converted to string
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not useToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?
– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
add a comment |
Ok, so you know how to use Timer
but your problem is with string
, when you use +
on string
you are concatenating
them, so say "hello" + " world"
is equal to "hello world"
, so when you concat i
with 00:00:00
the output you see is very logical.
you can use snippet below to achieve your goal(just replace your form class content with this)
private Timer _timer;
private Label _label;
private int _elapsedSeconds;
public Form1()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
_label.Text = time.ToString(@"hh:mm:ss");
};
_label = new Label();
Controls.Add(_label);
}
Edit
Tnx to @Juliet Wilson I edited how time is converted to string
Ok, so you know how to use Timer
but your problem is with string
, when you use +
on string
you are concatenating
them, so say "hello" + " world"
is equal to "hello world"
, so when you concat i
with 00:00:00
the output you see is very logical.
you can use snippet below to achieve your goal(just replace your form class content with this)
private Timer _timer;
private Label _label;
private int _elapsedSeconds;
public Form1()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
_label.Text = time.ToString(@"hh:mm:ss");
};
_label = new Label();
Controls.Add(_label);
}
Edit
Tnx to @Juliet Wilson I edited how time is converted to string
edited Jan 19 at 10:02
answered Jan 19 at 9:57
hessam hediehhessam hedieh
656412
656412
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not useToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?
– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
add a comment |
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not useToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?
– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use
ToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?– MickyD
Jan 19 at 10:00
Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use
ToString()
as per Juliet's answer instead of the fidly hour/min/sec calculation?– MickyD
Jan 19 at 10:00
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
@shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper
– hessam hedieh
Jan 19 at 10:05
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
downvote removed. thanks for improving your answer :)
– MickyD
Jan 19 at 10:06
add a comment |
I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),StopWatch
doesn't have any count-down ability at all.
– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
add a comment |
I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),StopWatch
doesn't have any count-down ability at all.
– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
add a comment |
I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.
I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.
answered Jan 19 at 9:33
GauravGaurav
58637
58637
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),StopWatch
doesn't have any count-down ability at all.
– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
add a comment |
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),StopWatch
doesn't have any count-down ability at all.
– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Please mark it as answer as it resolved your problem.
– Gaurav
Jan 19 at 9:54
Well no, not only wouldn't address the immediate problem (formatting),
StopWatch
doesn't have any count-down ability at all.– MickyD
Jan 19 at 9:58
Well no, not only wouldn't address the immediate problem (formatting),
StopWatch
doesn't have any count-down ability at all.– MickyD
Jan 19 at 9:58
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@MickyD, although user has stated countdown in question, but looking at code he implied something else
– hessam hedieh
Jan 19 at 10:00
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
@hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)
– MickyD
Jan 19 at 10:05
add a comment |