timer problem

Posted 12 October 2012 - 02:30 PM

Hello i'm fairly new to programming and i'm having a problem with a click counter. see , i want to make it so it counts down from ten and when it hits zero and it won't let you click anymore. Any help would be greatly appreciated!

Re: timer problem

Posted 12 October 2012 - 02:51 PM

So you literally meant timer. I thought you wanted to say counter. OK. Timer has Interval property, where you declare time between ticks in milliseconds. If you meant by counting down from 10 seconds, you can just set interval to 1000, and then display countdown in timer tick handling routine (as you started). It would be easier, if you'd subtract t starting from 10 and check if it is 0 after subtraction. By not letting user clicking... what do you mean by that? Not letting him/her click the form, click the buttons,...?

Re: timer problem

Posted 12 October 2012 - 03:05 PM

Take some time, and try to do some coding. You need to decide are you decrementing/incrementing t variable on timer tick, or on button click, or on both events. In other words: would you want to allow user click 10 times on button, display number of button clicks in label, and than prevent incrementing the value of click count if it reaches 10. Would you like to just stop counting, or disable button? Or are you giving the user 10 seconds only, and then disabling whole form? From your answers, I can't tell what your goal is.

Re: timer problem

Posted 12 October 2012 - 03:26 PM

Then set timer interval to 10000. You can do it in design, or by code: Timer1.Interval = 10000. It would be wise to do it in form load event handling routine. Then you need to start that timer, with it's .Start() method. This too should happen when form loads.

In timer tick handling method, you set button's Enabled property to false, and stop the timer by calling it's .Stop() method.

In button click event handling routine, you just simply increment value of t variable by one, and assign it to .Text property of label.

Re: timer problem

Posted 12 October 2012 - 04:45 PM

Your not decrementing the same variable. You don't need the a variable just display the t variable in the label. If you call .Stop on the timer in the tick event it turns it off the very first time the tick event is called - hence the reason it is not continuing down.

Re: timer problem

Posted 12 October 2012 - 05:02 PM

You're also going to want to change that interval so that every second you get an updated number. Right now you won't see anything happen for 10 seconds. I believe you want it to countdown the seconds so 1000ms is close to 1 second.