Lmoen
asked on
Returning from TForm1.Close() event
Hello. Just a problem I ran across while programming =>
How do I return from a TForm1.Close() event.
For example =>
when a user presses the window's X, then the program tells him the document has not been saved, and he presses cancel
Or =>
when a user decides to save the file, but then in the SaveDialog presses cancel, thereby indicating that he wants to return to the program
When a new file is opened, but the existing file needs to be saved, I also need to do the same check (i.e - do you want to save the file?), but that I can sit in a seperate procedure, or check the Sender... Concerning the TForm.Close() I'm stumped however...
Thanks for any help
Serdyn
How do I return from a TForm1.Close() event.
For example =>
when a user presses the window's X, then the program tells him the document has not been saved, and he presses cancel
Or =>
when a user decides to save the file, but then in the SaveDialog presses cancel, thereby indicating that he wants to return to the program
When a new file is opened, but the existing file needs to be saved, I also need to do the same check (i.e - do you want to save the file?), but that I can sit in a seperate procedure, or check the Sender... Concerning the TForm.Close() I'm stumped however...
Thanks for any help
Serdyn
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
getmarti - damn... you beat me by 4 mins ;)
On CloseQuery is the right point
CanClose would be allways true at start, germarti,
i would prefer something like this:
procedure TForm1.FormCloseQuery(Send er: TObject; var CanClose: Boolean);
var mresult : Integer;
begin
if memo1.Modified then
mResult := MessageBox(self.Handle,'Sa ve before Quit?','Confirm',MB_YESNOC ANCEL or MB_ICONQUESTION);
case mResult of
mrYes : If savedialog1.execute then
memo1.lines.savetofile(sav edialog1.f ilename);
mrCancel : CanClose := False;
end;
end;
meikl ;-)
CanClose would be allways true at start, germarti,
i would prefer something like this:
procedure TForm1.FormCloseQuery(Send
var mresult : Integer;
begin
if memo1.Modified then
mResult := MessageBox(self.Handle,'Sa
case mResult of
mrYes : If savedialog1.execute then
memo1.lines.savetofile(sav
mrCancel : CanClose := False;
end;
end;
meikl ;-)
oops a begin - end pair missed
corrected version
procedure TForm1.FormCloseQuery(Send er: TObject; var CanClose: Boolean);
var mresult : Integer;
begin
if memo1.Modified then
begin
mResult := MessageBox(self.Handle,'Sa ve before Quit?','Confirm',MB_YESNOC ANCEL or MB_ICONQUESTION);
case mResult of
mrYes : If savedialog1.execute then
memo1.lines.savetofile(sav edialog1.f ilename);
mrCancel : CanClose := False;
end;
end;
end;
meikl ;-)
corrected version
procedure TForm1.FormCloseQuery(Send
var mresult : Integer;
begin
if memo1.Modified then
begin
mResult := MessageBox(self.Handle,'Sa
case mResult of
mrYes : If savedialog1.execute then
memo1.lines.savetofile(sav
mrCancel : CanClose := False;
end;
end;
end;
meikl ;-)
OnCloseQuery works nicely with MDI forms too. The main form checks all of the child forms before calling its own OnCloseQuery.
Phoenix - Yeah...but Kretzschmar may have the better of us both..
Waiting.....
Waiting.....
gemati,
at least, you was the first on the right point of the preferred solution,
i just added a better example
meikl ;-)
at least, you was the first on the right point of the preferred solution,
i just added a better example
meikl ;-)
Kretzschmar: As I stated...You had the better of both Phoenix and me.
Anyway...I got a long way to go to catch up with you so I'll take what I earn.
GEM :->
Anyway...I got a long way to go to catch up with you so I'll take what I earn.
GEM :->
>I got a long way to go to catch up with you so I'll take what I earn.
go on, gem :-))
go on, gem :-))
---direct cut'n paste from d5 help
Occurs when the Close method is called or when the user chooses Close from the form's System menu.
type TCloseQueryEvent = procedure(Sender: TObject; var CanClose: Boolean) of object;
property OnCloseQuery: TCloseQueryEvent;
Description
Use OnCloseQuery to specify the conditions under which the form can close. An OnCloseQuery event handler contains a Boolean CanClose variable that determines whether a form is allowed to close. Its default value is True.
You can use an OnCloseQuery event handler to ask users if they are sure they really want the form closed immediately. For example, you can use the handler to display a message box that prompts the user to save a file before closing the form.
The TCloseQueryEvent type points to the method that determines whether a form can be closed. The value of the CanClose parameter determines if the form can close or not.
example from same d5 helpfile
procedure TForm1.FormCloseQuery(Send
begin
if MessageDlg('Close the form?', mtConfirmation,
[mbOk, mbCancel], 0) = mrCancel then
CanClose := False;
end;
--------------------------
as long as you do processing in the formclosequery event, you have control over whether the form closes or not.
hope this helps