Sign in to follow this  

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

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Right, but there is a way to tell if it's the X button. I used to implement that particular code... (had something to do with lparam or something if I remember right) I'll look for it if needed. The point is that winproc gives you another method of control. I found for my app that I wanted to override an x close at times. This is something I did thru the winproc.

Share this post


Link to post
Share on other sites
FYI, Something I wish I would have looked at back then (when I used the winproc originally) is that you can cancel the closing of the form from within the event as well and bypass all the Non-DotNet code of winproc methods...


void Editor_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}



Although I found winproc programming a must know to better understand windows and how it handles messages.

Share this post


Link to post
Share on other sites
A clean way to implement this in multiple forms can be found at Aaron Zupancic's blog (http://blog.devstone.com/aaron/archive/2006/11/29/2082.aspx). It attempts to replicate the functionality of the Visual Basic 6 event QueryUnload.

This code is his, not mine.


using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Devstone.Demo.Forms {

#if ( DEBUG )
public class FormBase : Form {
#else
public abstract class FormBase : Form {
#endif

public enum FormUnloadMode : int {
None,
FormControlMenu,
Code,
OperatingSystemShutDown
}

private bool _closeButton = false;
protected FormUnloadMode ulMode = FormUnloadMode.None;

public FormUnloadMode UnloadMode {
get { return ulMode; }
}

protected sealed override void OnClosing(CancelEventArgs e) {
// set the flag back to false so as to not prevent the WM_CLOSE message
// from changing the UnloadMode
_closeButton = false;

// ensure that the event will be properly raised in the derived classes
base.OnClosing(e);

// reset the UnloadMode flag
ulMode = FormUnloadMode.None;
}

protected override void WndProc(ref Message m) {
const int WM_CLOSE = 0x0010;
const int WM_SYSCOMMAND = 0x0112;
const int WM_ENDSESSION = 0x0016;
const int SC_CLOSE = 0xF060;

switch ( m.Msg ) {
case WM_CLOSE:
if ( !_closeButton ) ulMode = FormUnloadMode.Code;
break;

case WM_SYSCOMMAND:
if ( m.WParam.ToInt32() == SC_CLOSE ) {
_closeButton = true;
ulMode = FormUnloadMode.FormControlMenu;
}
break;

case WM_ENDSESSION:
ulMode = FormUnloadMode.OperatingSystemShutDown;
break;
}

base.WndProc(ref m);
}
}
}



Inherit from his class and check the FormUnloadMode for your form in your OnClosing event handler.

Share this post


Link to post
Share on other sites

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this