IF THEN comparator not being processed

I am writing a rudimentary drawing app for my own amusement. The app uses a canvas control on a window .
The user clicks on a button representing a set of primitives. This sets a variable to either ‘line’ , ‘rectangle’,’ circle’ and so on, so the app knows what to draw.
here is the code for the pushbutton action event.

dmode="line"// Dmode is the drawing mode

Once the mouse pointer begins moving on the canvas area it will not start drawing the shape until the mousedown event is triggered. mousedown code as follows;

PenDown=true// allow the canvas to draw spx=x// starting point for x spy=y// starting point for y return true// recognise the mouse up event

code for the mouse move event

if PenDown=true then// PenDown must be testing false because this block does not process epx=x// end point for x epy=y// end point for y self.Invalidate// fire the paint event end

when the user has drawn the desired shape and the mouse up event is fired, mouse movement on the canvas no longer draws anything.
mouse up event

PenDown=false// stop drawing
The problem that I am having is that an if then statement is not testing the boolean variable ‘PenDown’ or 'PenDown is testing false. If it is false, I have no clue why because there is nothing between mousedown, mouse move and mouse up that could set it to false. To add to my confusion, the PenDown value does not even appear in the snapshot values if I add a breakpoint.

Just before your if statement, place

system.debuglog str(PenDown) to see the exact value of the variable.

Thanks Michel. Done that, but it tests false.

So your test works, the if then simply does not meet the condition set.

You want to check the scope of PenDown, as well as where it is set True or false before it gets there.

try to set PenDown as Property of your Window or your Object and reference it with self.PenDown

The PenDown variable is a property of the form the canvas is on, so I know that the scope aspect is ok. I have also tested PenDown
in the mousedown event and it tests true, so I know that it is set in readiness of the mouse move event.

the only two things I am missing in your code are: the “end if” clause and your missing spaces between code and remarks

if PenDown=true then // PenDown must be Testung false because this block does not process
epx=x // end point for x
epy=y // end point for y
self.Invalidate // fire the paint event
end if

but without project file it’s hard to say something

Hi Tomas, I shouldn’t need “EndIf” should I? Surely ‘End’ should suffice or else the compiler would raise an error .The remarks were added for clarity on the forum. They don’t actually appear in my code.

please try “end if” not just end

tried that, still broken

Mmm would you dare to post your project file for download?

Windows or Mac?
I ask because some years ago I had a problem where the mousemove wasnt firing AT ALL under Windows, and I had to subclass the window to catch WM_MOVE events from windows API

So: if you add x = 2 before the IF statement, does the debugger hit that line?

I just created a very small test project under Windows that draws by setting pixel, it works just fine (as expected).

Now if you don’t post your project it would require a crystal ball to guess what could be going on with it.

Especially how a window property that is set to true in keydown can turn false a microsecond later in MouseMove.

Try removing the condition in MouseMove and simply see if it draws at all. Maybe the issue is with the drawing part that you have not shared.

I know that it draws because I have already done what you suggested. Never posted a project on a forum before. How do I do that?
Its a Mac project. BTW Jeff, tried your suggestion a bottle of jack Daniels ago.

[quote=135283:@chris benton]Michel,
I know that it draws because I have already done what you suggested. Never posted a project on a forum before. How do I do that?.[/quote]

Put the project file onto your dropbox or on a web site, and simply post the URL to it…

When you return true from MouseDown subsequent events go to MouseDrag, not MouseMove, then MouseUp ends the event series. That’s why that code doesn’t fire, it’s going to MouseDrag.

Maybe you can bypass that effect by not relying on the mouse up event of the canvas (what happens if the user leaves the canvas while drawing?), but draw as long as system.mousedown is true?

In which case

Should show that the event isnt firing because that line will never be executed. A breakpoint will prove it

The OP says

But maybe that refers to the Windows API solution I employed.

Will, Ulrich and Jeff. Indeed you were all correct in your suggestions. The mousedrag is the event to use for this solution.
All seems to be working as planned now. Just another trap for new users of Xojo.