Solved

Message Box on close

Posted on 2003-11-16
13
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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