[.net] Prompt quit question before closing using X button

Started by
12 comments, last by devronious 16 years, 3 months ago
Is it possible to trap when a user clicks he X button to quit the application and prompt the user "Are you sure you want to exit?". The reason I need to do that is my application runs on the taskbar when you minimize it but it will close when you press the X button. So I would like to prompt the user to ask if they want to exit incase they press the X button thinking it will minimize the application.
Advertisement
Try checking and handling the "Microsoft.Xna.Framework.Game.Exiting" event.

Or maybe see if you can overload "protected virtual void OnExiting (Object sender, EventArgs args)"

Otherwise I dont see anything in XNA you could use aside from running in fullscreen. If you using c++ and/or MDX then there are several option available such as intercepting the close command, removing or disabling the X button, or even change remove the title bar completely. Unfortunatley its been way to long since Ive done it so you will need to check you API documentation.
-------------------------------------------------------- What''s the difference between engineers and architects? Engineers build weapons; architects build targets. --------------------------------------------------------
Assuming you mean Windows Forms, here's a simple example:

Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);void MyForm_Closing(object sender, System.ComponentModel.CancelEventArgs e){    DialogResult r = MessageBox.Show("Really Quit?", "Quitting", MessageBoxButtons.OKCancel);    if (r == DialogResult.Cancel)        e.Cancel = true;    // Cancel the event}
Quote:Original post by Gage64
Assuming you mean Windows Forms, here's a simple example:

*** Source Snippet Removed ***


Great exactly what I need, thanks!

Actually one more question, is there any way to know if the X button was pressed as there are other ways the program will exit and I don't want it to prompt then?
You are going to want to use the Form.FormClosing event instead of the old Closing event because it will give you the source of the closing event (ie. Application exit programaticaly, User close, etc).
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.Popoloski
You are going to want to use the Form.FormClosing event instead of the old Closing event because it will give you the source of the closing event (ie. Application exit programaticaly, User close, etc).


It seems every way I close the form using FormClosing has a reason of "UserClosing". Whether I close it using Form.Close(), "Exit" from the MenuStrip or using the X button.

So the FormClosingEventArgs seems quite useless to me. Surely there must be a way to detect if the Form was closed using Form.Close() or the X button?
Why not have a flag that you set wherever you close the form with code. If that flag isn't set, the user closed it someway that didn't require code on your part.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Quote:Original post by Machaira
Why not have a flag that you set wherever you close the form with code. If that flag isn't set, the user closed it someway that didn't require code on your part.


Yeah good idea. It works fine, thanks :)
This isn't completely dotnet but I use the winproc method to override the x button in my app...

        /// <summary>        /// The WndProc method.        /// </summary>        /// <param name="m">A message.</param>        protected override void WndProc(ref System.Windows.Forms.Message m)        {            if (this.messageReady)            {                this.messageReady = false;                m.Msg = this.message;            }            if (m.Msg == WindowsSupport.Messages.WM_CLOSE)            {                //this tells the tool windows not to cancel their close events.                this.mainShutDownInProgress = true;            }            base.WndProc(ref m);        }


HTH,

Devin
The WM_CLOSE message can be generated a number of ways, not just by the Close button.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

This topic is closed to new replies.

Advertisement