Using canvas from a method

I have just converted my large project from Real Studio 2012 to Xojo 2019r2 and most of the code is executing fine after some tinkering.

The one thing I can’t seem to get working is my canvas related code…

I have a (global) method that handles my canvas stuff and for that I use the canvas as a parameter:

Globaal.ToonFoto Function ToonFoto(LokaalGrafics as graphics, Lok_Foto as string) As Picture

I can’t seem to get this to work. With the new Xojo approach to a canvas you need to put all code in the paint event?

(I am not a daily coder, only for one large project I manage and I have just switched from RS 2012 to Xojo 2019)

…and also calling a canvas from a method in the same window: I can’t seem to get it to work. :frowning:

You don’t. period.
All actions on a canvas must be done via that canvas PAINT event

this was changed sometime after 2012

I hope I understand your question correctly…

You can call a method (for example “DrawSomething”) from the paint event of the canvas by passing the graphics object to the method like this:

DrawSomething(g)

Of course the method “DrawSomething” needs to have a parameter “g as Graphics”.
Then in the method “DrawSomething” you can do your drawing onto g (the graphics object of the canvas), for example like this:

g.DrawRect(...) g.DrawPicture(ToonFoto, ...)

Everytime you call Canvas.Invalidate (for example from a timers “Action” event) the canvas will do the (re)drawing.

Also in this case, all drawing is done via the paint event, like Dave already said.

Strange thing is that I was able to run my RS2012 code in Xojo just about a year ago without too much changes. I was considering the switch then also, but the party I develop this project for was not ready for an investment yet. So this must have been removed in a recent Xojo update?

BUT: I was able to make a start in solving this…
I am now doing all my “old” stuff in a picture instead and draw that picture onto the canvas. This way I have my old code up and running in Xojo in a couple of days without having to do massive rewrites. I can see what I can improve from there on.

Also see:

https://docs.xojo.com/UserGuide:Updating_Code_That_Used_the_Graphics_Property

Thanks!

[quote=462435:@Dave S]You don’t. period.
All actions on a canvas must be done via that canvas PAINT event

this was changed sometime after 2012[/quote]
Now with 19r2, this is a “Have To” way to do it.

I feel the same as back then with threads updating GUI elements, but for me, these changes are fine.
Needs to updating a lot of code, but I’m not angry about.

Hint for programmers that want change graphics displayed in a Canvas:
Set a global variable, then call Canvas.invalidate now, instead of manipulating from within code.

this has been the “Have to” method for some years now… not new with R2

A quick and convenient way to replace Canvas.graphic by creating a new picture and adding it as Canvas backdrop.

You can then replace canvas.graphic by canvas.backdrop.graphic without changing anything else in your method.

Actually only for a year or so. I tried Xojo 2018 and that still ran my old code. The helptext also confirms that this was removed somewhere in 2018.

As I said: I’m coming from RB2012r1.2 so I have some catching up to do :wink:

As they already said: it was deprecated in the night of times.

It ws removed within the previous year.

Check within R2 IDE how many deprecated you have in your code (that “ran your old code” :wink:

There’s a lot deprecated, but most of it is still working in the newest Xojo. The only thing that I needed to fix right away was this canvas stuff, because that was not working anymore. But that is solved now! I used a local picture instead and was able to make it work with minor changes.

My project is running again and in the next few weeks I am going to replace old code with new.

Thanks for all the help here!!
:slight_smile:

Deprecate <> removed

Ah oke, thanks. English is not my first language and I was under the assumption it meant “removed”. But Google Translate explains that it means that it is still working, but is discouraged to use :slight_smile: