Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
New to coding and have looked everywhere for an example of this. I need to create a program that increments a label in 10s intervals. Once the number in the label has reached 60s I need it to pass over into the minutes label ("00:00:60" to "00:01:00") and so on.

Once a time has been decided, a start button is clicked and a timer counts down to 00:00:00.

The program is a microwave if that helps visually.
Any help would be amazing, thanks!

What I have tried:

//button1 is the 10s increment. label4 is seconds. label5 would be minutes.
private void button1_Click_1(object sender, EventArgs e)
{
int counter = int.Parse(label4.Text);
counter=counter+10;
label4.Text = counter.ToString();
Posted
Updated 17-Apr-17 6:56am
Comments
ZurdoDev 17-Apr-17 11:21am    
There are tons of examples online. Just use a timer.
Member 13133439 17-Apr-17 12:01pm    
there are timer examples yes, but I need it to do exactly what I said
ZurdoDev 17-Apr-17 12:04pm    
Then you have to write code for it. You won't find the exact code you need on the internet because every project is different. Take what you find and then tweak it to work with what you have.
Member 13133439 17-Apr-17 12:08pm    
I've been trying for about 4 days to create the code. The part I'm struggling with is the passing over of the seconds to the minutes label, not for the timer, but the increments to begin with

Start with a timer, and don't worry about counting.
Instead, try this:
C#
private DateTime startedAt;
private void StartTimer()
    {
    Timer ShowElapsedTime = new Timer();
    ShowElapsedTime.Interval = 10000;     // 10 seconds
    ShowElapsedTime.Tick += ShowElapsedTime_Tick;
    ShowElapsedTime.Start();
    startedAt = DateTime.Now;
    }

void ShowElapsedTime_Tick(object sender, EventArgs e)
    {
    TimeSpan diff = DateTime.Now - startedAt;
    txtHours.Text = diff.Hours.ToString();
    txtMinutes.Text = diff.Minutes.ToString();
    txtSeconds.Text = diff.Seconds.ToString();
    }
 
Share this answer
 
Comments
Member 13133439 17-Apr-17 12:05pm    
Hi, thanks for your input. Please may you be able to help me with the question I have asked!

Thank you so much
OriginalGriff 17-Apr-17 12:19pm    
Don't do it that way: it's not accurate for one thing, and it makes your life more complex. If you do it the way I suggested, the time is always correct and it's a lot easier as the difference between the start and current time is always worked out when you need it.

Trust me on this: I've been there, done that! :laugh:
Member 13133439 17-Apr-17 12:23pm    
In my head that is the only way I can work out how to do it with the parameters my lecturer has set.

Thanks anyway
[no name] 17-Apr-17 12:36pm    
"parameters my lecturer has set", there's your actual problem right there. We have no idea at all what limits your teacher put on you. We only know what *you* tell us. And you haven't described an actual problem yet and all you have shown us is some random bits of code that have nothing at all to do with whatever your problem really is.
OriginalGriff 17-Apr-17 12:49pm    
"NotPoliticallyCorrect" is right - we don't know what you don't tell us!
If your Tutor wants you to do it one way, then it's homework: which means we don;t give you code!
But... If you have a "seconds counter" - i.e. one you add ten seconds to each time you update your labels - working out the number of hours, minutes and seconds is easy:

hours = counter / (60 * 60);
minutes = (counter - (hours * 60 * 60)) / 60
seconds = counter modulus 60

Try it: see what you get.
Part of your problem is that you're using the controls (specifically a label) to hold your data. Don't do that. The controls should never be a part of your data model. They are there to edit and display the data in your model, not be your data model.
 
Share this answer
 

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