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)

Any ideas?

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


instead of