Instead of posting events for the form itself, simply override the OnFormClosing method. To display a confirmation message for confirmation, simply check the DialogResult value from the MessageBox:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
if (MessageBox.Show("Do you want to close?",
"Confirm",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No) {
e.Cancel = true;
}
}
base.OnFormClosing(e);
}
Be careful with such features, although this tends to annoy the end user.
source
share