Implementing timer in canvas mousedown event

I have a mousedown event that needs to iterate through a for statement. I know the way to do this is with a timer but damned if I can figure out the syntax. Here’s the MouseDown Code:

Var r As New Rect
For i As Integer = 0 To totalRows
  TestLbl.Text = i.ToString
  TestLbl.Refresh
  MouseDownTimer.RunMode = Timer.RunModes.Single
  r.Top = VDB(i,2)
  r.Left = VDB(i,1)
  r.Width = VDB(i,3)
  r.Height = 40
  If r.Contains(X,Y) Then
    Exit For
    MessageBox("Got It")
    MouseFound = 1
  Else
    MouseFound = 0
  End If
  
  MouseDownTimer.RunMode = Timer.RunModes.Off
  
  Return RectFound
Next

RectFound and MouseFound are properties to enable sharing with the Timer event.

Any help or suggestions would be greatly appreciated.

TestLbl only ever displays “0”

You’d want to Put the for loop in a method and use Timer.CallLater. Unfortunately that won’t help you here because you’re wanting to return a value.

Just out of curiosity, why are you worried about this? Unless you’re checking thousands of rectangles, you shouldn’t even feel the hit.

That’s what I thought (in this case there’s 35).

But my testing has shown me that, if I just work with a single Rect - ie, swap the VDB(i,n) for VDB(n,n) -the MouseDown works as expected but, as soon as I try to iterate, it stops. And, even though it seems to only be looking at i = 0, clicking on Rect(0) gets nothing.

Here’s the code without timer:

Var r As New Rect
For i As Integer = 0 To totalRows
  r.Top = VDB(i,2)
  r.Left = VDB(i,1)
  r.Width = VDB(i,3)
  r.Height = 40
  If r.Contains(X,Y) Then
    Exit For
    MessageBox("Got It")
    Return True
  End If
Next

Brilliant. Thank you. Didn’t need the timer in the end, just to outsource the loop to a method.

You understand that with that exit for, the message box and the return will never execute, right?

1 Like

Really? It certainly seems to work.

Thanks for the heads-up. I’ll reciew my code again. Obviously the messagebox is just for testing. Maybe I changed the order but it’s all working just dandy now.

1 Like

That couldn’t happen. If that

MessageBox("Got It")

fires, and that Return executes, then Xojo is broken.

As I say, the code I’ve written in the method must be in the correct order. Having made it work I’ve closed Xojo down for Christmas (in accordance with the “You ever want me to speak to you again?” law) :rofl:

I certainly see the logic here. I was swapping stuff around like a madman, trying to get it to work.

Have a happy Christmas