Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to start a timer when a button is clicked, end it when a different button is clicked, and show the amount of time passed between the 2 events. My code is below and the timer is declared in a private class. It starts in 1 event, ends in another, and is converted to int32. This is then added to an int to give me an integer of the elapsed time. The integer is then converted to a string and placed in the label. Unfortunately it only does the interval (1 second).
C#
private System.Timers.Timer Timer = new System.Timers.Timer(1000);
private void btnNewProblem_Click(object sender, EventArgs e)
{
     Timer.Start();
}
private void btnCheckAnswer_Click(object sender, EventArgs e)
{
    Timer.Stop();
    time += Convert.ToInt32(Timer.Interval) / 1000;
    lblTimer.Text = time.ToString() + " Second(s)";
 }

A few things to note:
1. I know timer.interval is wrong. I tried this after seeing it on several forums.
2. This is for a class. The instructor wants it to show in only seconds, and be in interval notation.
3. The instructor also wants it to be a timer and not a stop watch.
Thanks in advance!
Posted
Comments
Sascha Lefèvre 29-Mar-15 20:48pm    
As it's for a class I will only give you a hint so that you will have the opportunity to learn something :-D
Take a good look at this page, you should get a clue:
https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx
Member 11561872 29-Mar-15 20:54pm    
I checked that page already, but i don't see were it outputs it to a label? it outputs to the console with a separate event but i can't have that.
Sascha Lefèvre 29-Mar-15 20:58pm    
Don't bother with setting the label text yet. Your first step is to determine the time between buttonclick1 and buttonclick2. "Remember" you could do anything in the event-handler-method instead of what's shown in that example...
Member 11561872 29-Mar-15 21:21pm    
I still don't understand what I'm doing wrong. Based on everything we've been taught and all the research I've done my problem is occurring in the line:
time += Convert.ToInt32(Timer.Interval) / 1000;
But i don't know what to change. I would imagine I need to change Interval but anything i change it to breaks the code with the error "Input string was not in a correct format."
Sascha Lefèvre 29-Mar-15 21:30pm    
Timer.Interval is the time interval in which the timer fires the "Elapsed"-event which in that microsoft-sample calls the OnTimedEvent-method. It's always 1000 in your case, so even if that line wouldn't give that error, the result wouldn't be the interval between buttonclick1 and buttonclick2 but instead be always 1000/1000 = 1.

How could it be helpful to have the event-handler-method (from the microsoft-sample) be called every 1 second?

1 solution

Use the StopWatch[^] class.
C#
Stopwatch stopwatch = new Stopwatch();

public Form1()
{
    InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
    stopwatch.Start();

}
private void button2_Click(object sender, EventArgs e)
{

    stopwatch.Stop();
    var milliSeocnds = stopwatch.ElapsedMilliseconds;
    MessageBox.Show(millSeconds + "milliseconds passed between the two clicks.");
}
 
Share this answer
 
Comments
Member 11561872 29-Mar-15 22:09pm    
I can't use a stopwatch for this assignment
Sascha Lefèvre 29-Mar-15 22:30pm    
Abhinav, it would have been easy for me to provide him a solution over an hour ago. Instead I chose to direct him to find a solution because it's for his class. Apart from the fact that this is no solution for him it would have been respectful if you had refrained from posting at all.
Member 11561872 29-Mar-15 22:31pm    
For which i do thank you. I don't quite understand yet but i am learning!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900