How to have button draw on canvas?

I want to have the button pressed event draw on the canvas using “g.DrawRectangle(50, 50, 100, 100)”. The “g” item does not exist for the button.

Please show a simple example to do this. I have looked through the examples.
I do not want it in the Canvas paint event because the code for the button will be changed. This is just to get started.

you get the paint event by calling refresh method of the canvas.
from there you call any method with input of g as graphics.

alternate a picture object have a graphics context to paint.

Please explain with sample code.

As @MarkusR said, you get the Paint event of a Canvas by calling the Refresh method of the Canvas.

Try this example, to show the relationship between a calling object (a Button) and a Canvas, and get the Canvas to respond by drawing what you want.

Button-Canvas.xojo_binary_project.zip (5.5 KB)

Is there a way to put the g.DrawRectangle(50, 50, 100, 100) code inside the button press event?

Hi James,
Your request is not clear to me …
Do you want to draw a rectangle when you click the button?
Is the button already drawn on the canvas, or is the button separate from the canvas?
Do you want to draw on the canvas, not on the button?
Do you want to draw on the button which is on the canvas?
Or have I missed what you want to do?

The button and canvas are two complete separate controls. The four values of g.DrawRectangle(50, 50, 100, 100) will change according to some text boxes. I can take care of that part, but I need g.DrawRectangle(50, 50, 100, 100) it be in the the button press event.

Well, if you subclass the Canvas, you can add your own custom Method (or properties) and call that from your Button Pressed event.

Buttons do not have a Paint event. You cannot draw into them. Is that what you’re asking?

No.

1 Like

I think using a subclassed Canvas control is about as close as you’ll get.

Give this a try:
Button-Canvas v2.xojo_binary_project.zip (6.8 KB)

1 Like

Although drawing to the canvas ‘on demand’ was allowable 10/12 years ago, it is no longer possible.
So you cannot draw directly to the canvas when a button is pressed.

One thing you can do which may make it easier for you at this stage is to create a picture of the same size as the canvas when the window opens.
Make this a property of the window.

Var DrawingPicture as picture

In the canvas’ Paint event, add this code:

if DrawingPicture = nil then DrawingPicture = new picture (me.width, me.height)
g.drawpicture DrawingPicture,0,0,me.width, me.height

In your button Action event, (assuming the Canvas is named theCanvas)…

DrawingPicture.graphics.DrawRectangle(50, 50, 100, 100)
theCanvas.invalidate // or refresh
2 Likes

as Scot said,
sub class a canvas and add there a few properties and or methods.
as example a list of rectangles, it could also be a list of your own class/object.
the paint event use this collected list (propertie) and just paint it with for each.
means the button click push data into this own canvas, then call the canvas .refresh, the canvas use his data.

the suggestion with a picture use more memory. it’s up to you.

to draw at other methods we can forward the Graphics, its frequently useful to encapsulate.
grafik

grafik

1 Like