Solved

Returning from TForm1.Close() event

Posted on 2001-07-24
10
260 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:Lmoen
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 21

Accepted Solution

by:
gemarti earned 150 total points
ID: 6313569
In the OnCloseQuery event of the form put in the following code:

If CanClose = true then
  begin
    CanClose := false;
    .
    .
    .
    do something; //insert your code

  end;
0
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6313602
use the form.closequery event which fires before the form.close.  This allows you to program anything that needs to be done

---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(Sender: TObject; var CanClose: Boolean);

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
0
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6313617
getmarti - damn... you beat me by 4 mins ;)

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6313674
On CloseQuery is the right point

CanClose would be allways true at start, germarti,

i would prefer something like this:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var mresult : Integer;
begin
  if memo1.Modified then
    mResult := MessageBox(self.Handle,'Save before Quit?','Confirm',MB_YESNOCANCEL or MB_ICONQUESTION);
    case mResult of
      mrYes :  If savedialog1.execute then
                       memo1.lines.savetofile(savedialog1.filename);
      mrCancel : CanClose := False;
    end;
end;


meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6313708
oops a begin - end pair missed

corrected version

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var mresult : Integer;
begin
  if memo1.Modified then
  begin
    mResult := MessageBox(self.Handle,'Save before Quit?','Confirm',MB_YESNOCANCEL or MB_ICONQUESTION);
    case mResult of
      mrYes : If savedialog1.execute then
                memo1.lines.savetofile(savedialog1.filename);
      mrCancel : CanClose := False;
    end;
  end;
end;

meikl ;-)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6313821
OnCloseQuery works nicely with MDI forms too.  The main form checks all of the child forms before calling its own OnCloseQuery.
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6313986
Phoenix - Yeah...but Kretzschmar may have the better of us both..


Waiting.....
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6314062
gemati,
at least, you was the first on the right point of the preferred solution,
i just added a better example

meikl ;-)
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6314788
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  :->
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316051
>I got a long way to go to catch up with you so I'll take what I earn.

go on, gem :-))
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
URL for downloading Google Chrome for Win XP 2 155
Multiple image collision 13 75
tidtcpserver connection lost handle 2 84
How to Get Images From Server using App Tethering 11 29
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question