Windows Develop Bookmark and Share   
 index > Windows Forms General > Closing Form
 

Closing Form

When a user clicks the X on the forum to close it. I would like to ask 'are you sure you want to stop playing' before shutting down. So how can I do that?
Fata1Attack  Monday, August 28, 2006 1:22 PM
You can add a handler for the FormClosing event to ask the user "are you sure...", setting the FormClosingEventArgs.Cancel property to true if you don't want to continue closing the form.
Peter Ritchie  Monday, August 28, 2006 1:25 PM
You can add a handler for the FormClosing event to ask the user "are you sure...", setting the FormClosingEventArgs.Cancel property to true if you don't want to continue closing the form.
Peter Ritchie  Monday, August 28, 2006 1:25 PM
This is off topic for the Visual C# Express Edition forum, moving the the Windows Forms General forum.
Peter Ritchie  Monday, August 28, 2006 1:26 PM

I made this code and it now shows 2 times the messagebox don't see why it does it twice:

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            DialogResult dlgResultStop;

            dlgResultStop = MessageBox.Show("Are you sure you want to stop playing tetris?","Stop tetris?" ,MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation);

            if (dlgResultStop == DialogResult.Yes)

            {

                Application.Exit();

            }

            else if(dlgResultStop == DialogResult.No)

            {

                e.Cancel = true;

            }

        }

Extra: I would also like that all forms in my application are only able to be opened once in my application, can I use this event for it and how should I then?

thanx

Fata1Attack  Monday, August 28, 2006 2:09 PM
Fata1Attack wrote:

I made this code and it now shows 2 times the messagebox don't see why it does it twice:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

DialogResult dlgResultStop;

dlgResultStop = MessageBox.Show("Are you sure you want to stop playing tetris?","Stop tetris?" ,MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation);

if (dlgResultStop == DialogResult.Yes)

{

Application.Exit();

}

else if(dlgResultStop == DialogResult.No)

{

e.Cancel = true;

}

}

If this is the main form, you don't need the call to Application.Exit(). Since the form hasn't closed yet (and won't until your return from the FormClosing handler) the application is attempting the close the form again in the Application.Exit() call; which is raising the event a second timeand asking the question again.

Peter Ritchie  Monday, August 28, 2006 2:43 PM

You can use google to search for other answers

Custom Search

More Threads

• About the tab control
• user access control
• How to activate currently running windows app
• Crash with Doublebuffered property set to true to a listview
• GDI+ Error and (JIT) debugging message, help need
• Detecting ESC key stroke in ToolStripTextBox
• Property Grid Tab Order
• ToolStrip drop down menu to appear on a button click event
• RichTextBox OnDragDrop and OnDragEnter methods do not register Control key pressed
• Object relational mapping