Hey, I would like to show a prompt when a user clicks on the "X" button on my form, but it doesn't seem to be working.. Here is the code:

private void Form_FormClosing(object sender, FormClosedEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
      {
         MessageBox.Show("YES");
      }
}

Any ideas?

Recommended Answers

All 3 Replies

Code works fine here, message box shows, form closes, everyone is happy. Did you attach the method to the FormClosing event?

You can try putting a MessageBox as the first line just to see if the event is firing.

Yeah, that is so weird..

I have a login form (subform) and I don't want users just to be able to click on the X button and still be able to access the content..

Thanks for your reply

Are you writing this code in the main form or in the subform? and what is the exception you are getting because I am using this event in my application i have added quite a large lines of code in it and it works fine..... Try

System.Windows.Forms.FormClosingEventArgs

instead of

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