Commonly clicking the "X" in the corner of a dialog box closes the dialog box. How do I override the corner "X" so the dialog box does not close? See snipboard.io/kJZbzs.jpg
Loading
Commonly clicking the "X" in the corner of a dialog box closes the dialog box. How do I override the corner "X" so the dialog box does not close? See snipboard.io/kJZbzs.jpg
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Mar 9, 2024, 7:52 PM
If you're using a Windows Forms application, you can handle the FormClosing event of the Form to prevent it from closing when clicking the "X" button. With this code, clicking the "X" button will no longer close the form. You can still provide a button or other mechanism within your form to allow users to close it programmatically if needed.
Naimish MakwanaPosted Mar 8, 2024, 7:39 AM
In a Windows application, you can override the behavior of the “X” button in the corner of a dialog box by handling the
FormClosingevent. Here’s an example in C#:In this code,
e.CloseReason == CloseReason.UserClosingchecks if the form is being closed due to the user clicking the “X” button. If it is,e.Cancel = true;prevents the form from closing.Remember to attach this method to the
FormClosingevent of your form. You can do this in the form’s constructor or using the properties window in the designer:This will prevent the form from closing when the “X” button is clicked, and instead, it will show a message box saying “The form cannot be closed by clicking the ‘X’ button.”. You can replace this with your own logic as needed. Please note that this might not be the best user experience, depending on the context of your application. It’s generally recommended to allow users to close forms and applications as they wish. If there’s unsaved data or some other reason you want to warn them before closing, consider showing a confirmation dialog instead.
Thanks
Jignesh KumarPosted Mar 8, 2024, 3:50 AM
If you are using windows application, please use this one,