Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

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

This topic is 3934 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
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.

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
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).

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!