複数のフォームを持つ C# アプリケーションがあります。すべてのフォームに、キャンセル ボタンと右上 (x) ボタンがあります。すべてのフォームに対して、次のような cancelButton_Click() を作成しました。
private void cancelButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
これは、次のような formClosing 関数を呼び出します。
private void FormLoginPage_Closing(object sender, FormClosingEventArgs e)
{
string exitMessageText = "Are you sure you want to exit the application?";
string exitCaption = "Cancel";
MessageBoxButtons button = MessageBoxButtons.YesNo;
DialogResult res = MessageBox.Show(exitMessageText, exitCaption, button, MessageBoxIcon.Exclamation);
if (res == DialogResult.Yes)
{
e.Cancel = false;
}
else if (res == DialogResult.No)
{
e.Cancel = true;
}
}
同様に、すべてのフォームに対して Closing イベントを異なる方法で処理したいので、すべてのフォームに対してカスタマイズされた formClosing 関数を作成しました。
ただし、たとえば Form2 の [キャンセル] ボタンをクリックすると、コントロールは Form1 Closing イベント関数に移動します。これを実装するより良い方法はありますか?提案してください。