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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 7, 2024, 10:50 PM EST
If you are using windows application, please use this one,
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; // Cancel the closing event for dailog box }Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 8, 2024, 2:39 AM EST
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#:private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; // This will prevent the form from closing MessageBox.Show("The form cannot be closed by clicking the 'X' button."); } }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:public Form1() { InitializeComponent(); this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); }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
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 9, 2024, 2:52 PM EST
public partial class Form1 : Form { // Constructor or initialization method public Form1() { InitializeComponent(); this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); // Attach the event handler } // Event handler for the FormClosing event private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; // Cancel the closing action } }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.