[Solved] Alert on Closing window in Window Form Application in C# [duplicate]


You can find this by a simple search!

Handle Closing event of your form:

this.Closing += OnClosing; // For example put this in the constructor of your form

private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
        string msg = "Do you want to close this?";
        DialogResult result = MessageBox.Show(msg, "Close Confirmation",
            MessageBoxButtons.YesNo/*Cancel*/, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
            /* Do something */;
        else if (result == DialogResult.No)
            cancelEventArgs.Cancel = false;

}

11

solved Alert on Closing window in Window Form Application in C# [duplicate]