Solved

Message Box on close

Posted on 2003-11-16
13
374 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
Technology Partners: 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

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!

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

617 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