i hate to be a person who asks for full help, but am a little stuck and am quite new to XOJO …
How do you get a timer to show text that counts down from a value.
For example:
dim countdowntime as integer
countdowntime = val(Textinputbox.text)
// PASS This to a timer that updates a label text counting down from the value in the Textinout box (in Mins)
E…g. I place the number 4 in the input box, i want to get a countdown from the number 4 to 0 showing in the label text .
Set the period of your Timer to 1000. Set it’s mode to multiple (2).
There are actually a lot of ways to attack your question, depending on how accurate you need the timer to be, if you want it to do some action when it gets to zero, if you’d prefer to instantiate the Timer in code and keep as a property of the window rather than a control, etc.
You’re trying to do it synchronously. All that’s going to do is hang up your app until the for loop completes in roughly 10 seconds. You need to do your countdown asynchronously to get it to work how you want. See my reply above.
Really appreciate the help on this one! Could you help me a little more please?
I really need it to count down from minutes then down to seconds. The value given in the text field for example if 2 mins, i really need it to show: 1:12 (1min, 12 seconds)
Also, if i do the following:
so take value from 1 and countdown in another, the countdown doesn’t run…
The count down should run if you set the period of the Timer to 1000 (ms) and the mode to multiple (2).
If you want to show minutes and seconds, create a property in your window, countdowntime as integer. Set it’s default value to 120 (i.e. 120 seconds). Then adjust the Timer.Action code as follows:
[code]dim mins, secs as integer
// self is not needed here, but reminds you it’s a window property
self.countdowntime = self.countdowntime - 1
mins = self.countdowntime \ 60
secs = self.countdowntime mod 60
TextField4.text = str(mins, “0”) + “:” + str(secs, “00”)[/code]
Yes, the 120 makes it so the timer takes 2 minutes to count down since 2 minutes=120 seconds.
In your submit button, you should put:
self.countdowntime=val(inputTextfield.text) 'time in seconds
and the timer will count down the time specified in seconds. If you want to be able to enter 5:30 to mean 5 minutes and 30 seconds, that will be a bit more complex, but we can show you how.
Also, all of us were noobs at one point or another so don’t worry to much about the questions.
Here is the code that would allow you to enter a time like 5:30 and have it properly register, note that this doesn’t work for hours but could be easily expanded if you needed it to work for hours also.
//IMPORTANT: The time must be entered with minutes and seconds for this to work, otherwise an
//OutOfBoundException will be raised, e.g. 4 minutes would be entered as 4:00
dim minutes, seconds, totaltimeinseconds as integer
dim thetime, tmpstring() as string
thetime=TextField4.Text
tmpstring=thetime.Split(":")
minutes=val(tmpstring(0))
seconds=val(tmpstring(1))
totaltimeinseconds=(60*minutes)+seconds
self.countdowntime=totaltimeinseconds
The code takes the string entered into the textfield and splits it at the “:” using the split function. Since the split function returns an array of strings, a temporary array, tmpstring(), is created to hold the values. Then the indexes of the values in the array are used to retrieve the values in the array which are converted into integer. Since, the countdowntime variable stores the time in seconds, then the minutes are converted into seconds. Finally, the time entered is stored in the countdowntime variable to be used by the timer.
The code should be put into the submit button that you use to initialize the timer. If it is in the action event of the timer, then it will fail. Here is my quick example. It is probably better to have the time remaining shown in another textfield or a label so that you can change the time you entered and not have it constantly updated by the timer to prevent you from changing the value.
I do have another question. I want to have a hidden text filed that where i have a static 10 second (00:10) set, i want to be able to add these the main time (which we have done above) to this.
I have tried a few ways now but seem to be coming up short.
Firstly i did this:
dim i as integer
BothAdded.text=str(val(Minutes.text)+val(Hidden.text))
The above was run off a submit button. All i kept getting back was 1 . I tried playing with the static value, setting as 0.10 / 00.10 / even 1:10. But each time i added them i either got 1 or 2 - So i can only assume its rounding it up?
Are you able to shed any light on this for me please .
Are you trying to make it so that when you click a button a certain amount of time (10 seconds) is added to the time in the timer? I’m not exactly sure of what you are trying to accomplish.