?
Solved

Easy question : How to let messagDlg respond to cancelclicks on closing form?

Posted on 2003-03-24
6
Medium Priority
?
205 Views
Last Modified: 2010-04-04
Hi there!

This should be an easy one :
In my application I use the messagedlg(...) to ask for user confirmation when they try to close the form (are you sure? Yes/No)

Now when I close the form through the FileMenu (TMainMenu->Exit) the messagedlg responds to the cancelclick.

But when I press the close-cross (in top-right corner) the popu doesn't respond to cancelclicks, and just blatantly closes the entire application.

Is there a big difference, and/or am I forgetting something?

btw, here's the messageDlg part I used :

if MessageDlg('This will close the program',mtConfirmation,mbOk,      mbCancel], 0) = mrOk then
begin
  Application.Terminate;// The end of the progame.
  AdminMain.ClearObjectList;
end
else ;  // Empty statement to let program respond to cancelclick

I think it has to do with the empty statement, is there another way to do this?

Thanks in advance

Robin
0
Comment
Question by:Binsky
[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
  • 3
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 8193598
you should place the dialog in the onclosequery-event
of your mainform and modify the canclose-paramater to your
needs -> in case of cancel -> canclose := false;

further advice:

in your exit-menu-click-event just place only the close-method

supply the onclosequery event like

canclose := MessageDlg('This will close the program',mtConfirmation,mbOk,      mbCancel], 0) = mrOk ;  

move this like
into the formdestroy-event of the main-form

AdminMain.ClearObjectList;

meikl ;-)
0
 
LVL 2

Expert Comment

by:Ratje
ID: 8193722
or try using this :

procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;

procedure TForm1.WMSysCommand;
begin
  if (Msg.CmdType=SC_ClOSE) then
    // show your dialog
  else
    DefaultHandler(Msg);
end;

or to disable the 'x'-button :

procedure TForm1.FormCreate(Sender: TObject);
var
  hMenuHandle: Integer;
begin
  hMenuHandle := GetSystemMenu(Handle, False);
  if (hMenuHandle <> 0) then
    DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);
end;

Rat
0
 
LVL 1

Author Comment

by:Binsky
ID: 8194780
Hi Kretzschmar! (and others of course...)

It took me a bit to find out exactly what you meant, (I wasn't reading it right, it was still a bit too much of the monday morning I guess) but that was exactly it!

I was trying to add your idea to the onclose event, which was essentially the same as what I had...But after my lunchbreak I actually saw what you wrote down...Tried it, and it worked (but you knew that!) The onclose will always close, except if the canclose bool is false. Good trick!

I also found out why my mainmenu=>exit_click did work, it didn't call the onclose, which windows "buttons" do!

Another day, another lesson!

Thankx, the 50 points are yours.
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!

 
LVL 1

Author Comment

by:Binsky
ID: 8194788
(I did it the wrong way around I guess, read comment above for actual comment.)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8194821
?? confused

well glad you got it work,
even i don't know yet, how

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8194834
btw.
mondays are really hard days for me too :-))
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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