Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Message Box on close

Posted on 2003-11-16
13
Medium Priority
?
380 Views
Last Modified: 2010-05-19
I just can't figure out how to have a message box like "Do you really want to quit?" displayed when i close the program...
Can someone help me plz ?
Thanx in advance
0
Comment
Question by:Professor
  • 5
  • 4
  • 4
13 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9758240
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
      CanClose := (MessageDlg('Are you sure you want to quit?', mtConfirmation, [mbYes, mbNo], 0) = mrYes);
end;

Geoff M.
0
 

Expert Comment

by:Shinez
ID: 9758255
Try this

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);

begin
  if MessageDlg('Close the form?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel then
     CanClose := False;
end;
0
 

Author Comment

by:Professor
ID: 9758468
That still doesn't ask me when i close the form though ...
I don't know if this is difficult or if im just too stupid *lol*

Its about like this vb6 code:

sub Form1.Unload(Cancel as Boolean)
 res=msgbox("Quit?","whatever",vbYesNo)
 if res=vbYes then
  Cancel=true
 end if
end sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Shinez
ID: 9758472
vb6? you want delphi OnCloseQuery?

it works fine play with it experiment with diffrent ways of doing it in delphi, there is not just 1 way of doing things,
0
 

Expert Comment

by:Shinez
ID: 9758479
if your using delphi then on your form in "Object Inspector" make sure you have form1/anything in the dropdown box above and then click events, then look for OnCloseQuery and then double click on the space next to it and then put the code above into the OnCloseQuery in your code area.
0
 

Author Comment

by:Professor
ID: 9758696
Still not showing anything...
0
 

Author Comment

by:Professor
ID: 9758705
Or did something change from delphi 6 to delphi 7?
0
 
LVL 8

Accepted Solution

by:
gmayo earned 1000 total points
ID: 9758706
Your unit should look something like this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ImgList, Menus;

type
  TForm1 = class(TForm)
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
      CanClose := (MessageDlg('Are you sure you want to quit?', mtConfirmation, [mbYes, mbNo], 0) = mrYes);
end;

end.

The FormCloseQuery is called whenever the form is about to be closed. Setting the CanClose to false will prevent it closing. You need to ensure that the form's FormCloseQuery event handler (in the object inspector as Shiner said) is pointing to the procedure.

Geoff M.
0
 

Expert Comment

by:Shinez
ID: 9758709
are you sure your using delphi, before you showed vb6 source.
0
 

Expert Comment

by:Shinez
ID: 9758715
ya gmayo you sort him out, I got to make sunday roast.
0
 

Author Comment

by:Professor
ID: 9758721
Yes I was actually using delphi ... And it works now thanks very much :)
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9758722
My example was on Delphi 7, however, it's been the same since at least Delphi 4.

Geoff M.
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9758755
BTW you can delete the other question and get your points refunded by posting in http://www.experts-exchange.com/Community_Support/ with the question URL and a message saying "please delete/refund".

Geoff M.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

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