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.
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.
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
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.