Question countdown timer problem!

aksl

Member
Joined
Jan 31, 2014
Messages
11
Programming Experience
Beginner
I found the following countdown timer code from another web site. It works pretty good.

1 Dim startdate As DateTime = DateTime.Parse(Date.Now)
2 Dim enddate As DateTime = DateTime.Parse("20104-02-24 18:00")
3 Dim ts As TimeSpan = enddate.Subtract(startdate)
4 Label1.Text = CStr(ts.Days) & " Days, " & CStr(ts.Hours) & " Hours, " & CStr(ts.Minutes) & " minutes, " & CStr(ts.Seconds) & " seconds!"

i want to stop timer on end date.
please add a code to stop timer on end date & open another form (ex: form 3)on that day automatically for me

thanks in advance!
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
icon_e_sad.gif
 
How about we explain what to do and you try it for yourself? Programming is about using your knowledge to write code that does what you want, not copying and pasting code snippets that you find on the web and getting other people to write the rest for you.

That fourth line is quite ugly and could be improved considerably. Firstly, all those CStr operators are useless. The string concatenation operator (&) is defined for types String and Integer and all those TimeSpan properties are type Integer. Get rid of all the CStr operators and it will work exactly the same. Some people might think that that would require implicit conversion of the Integers to type String but it does not. It is not the case that the Integers are implicitly converted to Strings first and then passed to the & operator. The operator itself accepts the Integer and explicitly converts it to a String. Secondly, you shouldn't be using & anyway. The String.Format method is specifically intended to make concatenating multiple values like that easier to read. You should read the MSDN documentation for that method and, if required, look for examples of its use online. Personally, I use it pretty much every time I would otherwise have to use more than one & on the same line.

As for the question, If you want to stop the Timer when the time has elapsed then you simply call Stop on the Timer at that time. Similarly, if you want to display a form then create a form and display it. If you don't know how to do that then I suggest that you follow the Tutorial link in my signature below and learn how. As for when to do it, what would tell you that the time has elapsed? Think about it. It's not a programming question because the same would be true if you were doing this with a pen and paper. If the current date/time is greater than or equal to your reference date/time then time has expired. That's a simple comparison. Also, if the time difference is less than or equal to zero then time has expired. That's also a simple comparison, using TimeSpan.Zero. You just make one of those two comparisons and, if time has expired, you stop the Timer and display your form.

Have a go at that and if it works then you're done. If it doesn't work then you can always post back here again, show us what you've done and explain your logic and we can help you fix the issue. The important thing is that you use your brain to try to solve the problem because the best way by far to learn is to do.
 
I am agree with you. But i am a very bigginer of vb.net. I am studying about vb.net coding by testing some collection of code snippets collected from lot of web sites.Doing this I can understand easily how codes working & their behaviour. this is my way of learning!
:upset:
Therefore please be kind to fulfill my request my brother !!!!!!!!!!!!!!!!!!!!!!!:upset::upset::upset::upset::upset:
 
That's the lazy way to think you're learning. Don't forget that I used to be a beginner too. Many's the time that I read some code and was completely convinced that I understood what it was doing, and I was often right that I understood it, but when it came to writing my own code to do something even slightly different I couldn't. Of course examples are a useful tool but the best way to learn how to write code is to write code. If you can't be bothered to make the effort then why should we? You don't have to get it right. We're here to help you if you have issues. You do need to make an effort though. If you think you don't then we can't be friends.
 
Back
Top