Solved

Message Box on close

Posted on 2003-11-16
13
365 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now