私の中には2つのボタンがありますWinForm(C#File -CloseとRed X)。両方を同じように動作させたいので、2 つの異なるイベントが発生したときに呼び出されるヘルパー メソッドを作成しました。これが私がこれまでに持っているものです。
private void CloseFileOperation(object e)
{
    // If the user selected the exit buttton from the main title bar,
    // then handle the closing event properly.
    if (e is FormClosingEventArgs)
    {
        FormClosingEventArgs FormCloser = (FormClosingEventArgs)e;
        // If the Spreadsheet has been changed since the user opened it and
        // the user has requested to Close the window, then prompt them to Save
        // the unsaved changes.
        if (SpreadSheet.Changed)
        {
            DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            switch (UserChoice)
            {
                case DialogResult.Yes:
                    SaveFileOperation();
                    FormCloser.Cancel = false;
                    break;
                case DialogResult.No:
                    FormCloser.Cancel = false;
                    break;
                case DialogResult.Cancel:
                    FormCloser.Cancel = true;
                    return;
            }
        }
        // If the Spreadsheet hasn't been changed since the user opened it, then
        // simply Close the window.
        else
            FormCloser.Cancel = false;
    }
    // Otherwise the user must have selected the "Close" option from the File menu.
    // Handle the event in the following manner.
    else
    {
        // If the Spreadsheet has been changed since the user opened it and
        // the user has requested to Close the window, then prompt him to Save
        // the unsaved changes.
        if (SpreadSheet.Changed)
        {
            DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            switch (UserChoice)
            {
                case DialogResult.Yes:
                    SaveFileOperation();
                    this.Close();
                    break;
                case DialogResult.No:
                    this.Close();
                    break;
                case DialogResult.Cancel:
                    return;
            }
        }
        // If the Spreadsheet hasn't been changed since the user opened it, then
        // simply Close the window.
        else
            this.Close();
    }
}  
ユーザーがRed X & File-Closeオプションを選択すると、この関数が呼び出されます。ユーザーが File-Close を選択すると、Close()操作で適切なアクションが呼び出され、それがさらに終了イベントで呼び出されることに注意してください。この結果、「変更を保存しますか?」という警告ダイアログが 2 回表示されます。
ウィンドウが2回表示されるのを防ぐ、現在の方法と同様の簡単な方法はありますか?