現在、32 ビット システムから 64 ビット システムに移行する際に、コードのアップグレードと再モジュール化を行っています。当然のことながら、私たちの目標の 1 つは、例として追加された Init() 関数から変更することです。
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form_Closing);
Windows イベントでこの種のことを処理したいと思います。そこで、Windows フォーム イベントの Form_Closing イベントに行ったところ、[驚くこともなく]これが form_closes イベントではないことがわかりました。これに対する私の質問は、CancelEventArgs と FormClosingArgs で実際に起こっていることの間に何か違いがあるのでしょうか、それともこれらの 2 つのコードは、一方がシステムのコンポーネントであり、もう一方が Windows イベント処理の結果であり、文字通り同じことを行っているのでしょうか。それは何を最もよくしますか?私はインターンとして、この新しいプロジェクトに飛び込んで夢中になっているだけです。データや問題を失うことなく、CancelEventArgs を FormClosing に置き換えることは可能ですか?
コード 1: CancelArgs
private void Form_Closing(object sender, CancelEventArgs e)
{
// If the user hit Cancel, just close the form.
if (this.DialogResult == DialogResult.Ignore)
return;
if (this.DialogResult == DialogResult.OK)
{
// If the address is not dirty, just cancel out of
// the form.
if (!this._editedObject.IsDirty)
{
this.DialogResult = DialogResult.Cancel;
return;
}
// Save changes. If save fails, don't close the form.
try
{
SaveChanges();
return;
}
catch (Exception ex)
{
ShowException se = new ShowException();
se.ShowDialog(ex, _errorObject);
_errorObject = null;
e.Cancel = true;
return;
}
}
Form_Closing -- 優先ルート
private void ScheduleItemDetail_FormClosing(object sender, FormClosingEventArgs e)
{
// If the user hit Cancel, just close the form.
if (this.DialogResult == DialogResult.Ignore)
return;
if (this.DialogResult == DialogResult.OK)
{
// If the address is not dirty, just cancel out of
// the form.
if (!this._editedObject.IsDirty)
{
this.DialogResult = DialogResult.Cancel;
return;
}
// Save changes. If save fails, don't close the form.
try
{
SaveChanges();
return;
}
catch (Exception ex)
{
ShowException se = new ShowException();
se.ShowDialog(ex, _errorObject);
_errorObject = null;
e.Cancel = true;
return;
}
}
}