Pressing the X Button
-
Hello everyone, I was wondering if there was a way to know when the X button was pressed at the upper right hand part of the screen Thanks, The Major Rager
-
Hello everyone, I was wondering if there was a way to know when the X button was pressed at the upper right hand part of the screen Thanks, The Major Rager
-
The Closing and Closed events of the form both are activated by this click. Closing happens first and will allow you to abort the shutdown.
Do you have an example of how I would code something in that particular function?
-
Do you have an example of how I would code something in that particular function?
You can do this inside the form itself, or using another object which has subscribed to the forms event. 1st within the form:
using System.Windows.Forms; using System.ComponentModel; class MyForm : Form { private bool stopUserFromClosingForm = true; public MyForm() { } protected override void OnClosing(CancelEventArgs e) { if(stopUserFromClosingForm) { e.Cancel = true; } } }
or if another object has a reference to the formstatic void Run() { Form myForm = new Form() myForm.Closing += new CancelEventHandler(this.HandleFormClose); myForm.Show() } private void HandleFormClose(object sender, CancelEventArgs e) { e.Cancel = true; // stop the form closing }
There is also a Closed event, which is juts a normal EventHandler - it has no Cancel property. -
You can do this inside the form itself, or using another object which has subscribed to the forms event. 1st within the form:
using System.Windows.Forms; using System.ComponentModel; class MyForm : Form { private bool stopUserFromClosingForm = true; public MyForm() { } protected override void OnClosing(CancelEventArgs e) { if(stopUserFromClosingForm) { e.Cancel = true; } } }
or if another object has a reference to the formstatic void Run() { Form myForm = new Form() myForm.Closing += new CancelEventHandler(this.HandleFormClose); myForm.Show() } private void HandleFormClose(object sender, CancelEventArgs e) { e.Cancel = true; // stop the form closing }
There is also a Closed event, which is juts a normal EventHandler - it has no Cancel property.Thanks for replying, but I am using ASP.NET, which means Web Forms instead of just a Window Form, do you have an example for that?