Listbox not visible at start of code

I’m sure there is a simple answer to this, but I’m fairly new to Xojo and need help please!

I’m creating a timer which is to display elapsed time. I have a ListBox with 3 columns, for H M & S.

At present I’m only concerned with displaying the seconds in Col 2 (3rd column) of the Listbox. The rollovers to M & H come later!

The code has a Do Until - Loop which updates the elapsed time & should display this in the Listbox.

At present, until it works, this runs for 10 seconds.

What happens at the moment is that when the Start button is clicked, nothing is displayed in the Listbox, until it exits the Do loop after 10 seconds or so, when it displays 11. I have seen from the debugger that the variable ‘elapsed’ is incrementing until it gets to 10, when the loop ends. The line inside the Do loop to update the listbox does not work.

My problem is that the Listbox does not display when I run the app or when I click the Start button.

The code I have is in the Action for a Start Button. My guess is that it should be somewhere else, and that’s what I’d like someone to tell me if possible.

The code in the Start button Action is this, and there is no other code.


Var Hrs, Mins, Secs, elapsed, current as Integer = 0 ’ not all used yet!
var start as Integer = System.Ticks / 60 ’ = seconds

Listbox1.AddRow
Listbox1.FontSize = 20
Listbox1.CellValueAt(0, 2) = “0” 'This does not display

Do Until elapsed > 10 ’ how long to run, in seconds

current = System.Ticks / 60

elapsed = current - start

Listbox1.CellValueAt(0, 2) = elapsed.toString ’ Does not display

Loop

’ Updated Listbox appears at this point, when ‘elapsed’ is > 10


Thanks in advance for a solution!

You probably need to do this in the CellTextPaint event instead, and in your code above replace this line with something like:

Listbox1.InvalidateCell (row, col)

Edit: plus, your code is running in a tight loop so your listbox doesn’t get a chance to update. You should add a sleep in there.

Maybe it can be a bit “hard” because you’re just starting… but I suggest you to take a look at the documentation here (specially under the "Updating the User Interface from a Thread" section).

Also, take a look to the project you can find under Xojo > Example Projects > Desktop > Threading so you can better understand how this works.

It makes a difference! :ok_hand:t2:

Thanks so much - I’ve had a quick look and see it is more complicated than I’d expected or hoped! But I’m certainly prepared to try to find out what I should be doing, and I’ll return if I get completely stuck

1 Like

It looks like you are trying to create your own timer. Why not use a Timer instead?
Drop a Timer on your form and set Run Mode off and Period to 1000
Then put the following code in the Start button action event

listbox1.addrow
listbox1.FontSize = 20
Timer1.RunMode = timer.RunModes.Multiple
Timer1.Enabled = TRUE

Then add this code to the Timer1 Action event.

Static elapsed as integer = 0
Listbox1.CellValueAt(0,2) = elapsed.tostring
if elapsed > 10 then
  me.Enabled = false
else
  elapsed = elapsed + 1
end if

That should get it working.
Thanks

1 Like

Thanks Tim for your help - the advice must be good coming from one with our shared name!

I can’t quite follow how to use InvalidateCell, or CellTextPaint, but I’ll read further & hope to find a solution!

Thanks Gary - my initial thought about my problem was that a Timer was the right way to go, but something I read suggested it was not suitable. But you have proved that wrong, and the (fairly) simple solution you have provided works exactly as required - thank you so much