Hi guys i am trying to catch the close event(press the X button) on a window form.
I try this

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
      // Display a MsgBox asking the user to save changes or abort.
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   
        }

But do nothing.
Any ideas??? What i am doing wrong

Recommended Answers

All 8 Replies

Thank you guys its working

>But do nothing.
I think FormClosing event is not handled properly. Add handler for Form's FormClosing event. (Look at property windows).

Thnxs for replying, could you be more specific i am totally newbie with this.
Thnxs in advance

Thnxs for replying, could you be more specific i am totally newbie with this.
Thnxs in advance

You can try this, may it help you......
On form closing event write this code

DialogResult ret;
                    ret = MessageBox.Show("Are you want to Exit!", "Exit".ToUpper(), MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    if (ret == DialogResult.Yes)
                    {
                        Application.Exit();
                    }
                    else
                    {
                        return;
                    }

When you say nothing happens, does the code in the event handler not work or does it not run?
Have you checked that the event handler has been bound to the event?
Chcek out my tutorial, let me know if it clears things up :)

Thnxs again for thw quick answers, but the problem is that i CANT catch the closing event the code in the block
Form1_FormClosing(){..} doesn't seem to be execute....

When you say nothing happens, does the code in the event handler not work or does it not run?
Have you checked that the event handler has been bound to the event?
Chcek out my tutorial, let me know if it clears things up :)

Worked!!!
I select my form then press the lighting bold on the properties window, find the ClosingEvent , double click and open the code editor window with the block of closing event... EXCELLENT TUTORIAL 10/10 ;)
And again thank you!!!

No problem, just glad it helped. Made it worth the few minutes to write it :) You'd be suprised how many people write the event handlers without binding them to an event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.