A simple project:
the window1 has a property: SaveFlag as boolean
the window1 has a canvas with the following paint event code
Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
If SaveFlag = True Then
Var dlg As New SaveFileDialog
Var saveFile As FolderItem
dlg.InitialFolder = SpecialFolder.Documents
dlg.PromptText = "Prompt Text"
dlg.SuggestedFileName = "Suggested Filename"
dlg.Title = "Title Property"
// dlg.Filter = FileTypeGroup1.Text // defined as a file type in FileTypeGroup1 file type set
saveFile = dlg.ShowModal
If saveFile <> Nil Then
// saveFile is the FolderItem of the file to save
Else
// user canceled
End If
SaveFlag = False
End If
End Sub
the windwo1 has a button:
Sub Action() Handles Action
SaveFlag=true
Canvas1.Invalidate(false)
End Sub
Xojo crashes at saveFile = dlg.ShowModal
I wonder if it is not possible to cal dlg.ShowModal from inside Paint event?
caling a file dialog in the paint event is the wrong place.
create a method with g As Graphics Parameter
call this method in the Paint event
if you need the canvas picture use this new Method to paint into a picture.graphics.
Move the code from the paint event into a new method (for the purposes of this example I’ll call it DrawMe). This method should have a parameter g As Graphics.
In your current paint event call DrawMe(g).
In your save method create a new picture and then do DrawMe(mypicturevar.Graphics). At this point you can then do whatever to save the picture.
you could create the new picture object with
any size. this obj then have a graphics property.
any method with graphics argument can paint there.
as example from button event
Sub Action() Handles Action
Var pic As New Picture(256,64)
DrawMe(pic.Graphics)
Self.Backdrop = pic 'just a test to view it
End Sub
Public Sub DrawMe(g As Graphics)
g.DrawingColor = Color.Red
g.FillRoundRectangle 0,0,g.Width,g.Height,32,32
End Sub
in the paint event of your canvas you can call the same method DrawMe. keving explained it.
paint event draw something
save dialog open a dialog, draw something, save the picture
this concurrence can use the same method.
The user does not draw something.
YOU do, based on what they tell you.
There is no ready made picture sent to you in the Paint event.
g is an empty page
You must draw on it in paint
if you want the image saved to disc, either draw the image to G and also draw it to a picture (to save),
OR
draw to the picture (for saving) , and then throw the picture at G using drawpicture.
We are all telling you the same thing.
G does not contain your picture.
The Canvas is not a sheet of paper that remembers where someone drew on it.