从32位系统转移到64位系统时,我们目前正在升级和重新调制代码。在适当的过程中,我们的目标之一是从Init()函数更改为其中添加了示例。
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form_Closing);
我希望Windows事件可以处理这些事情。因此,我进入了Windows Form Events中的Form_Closing事件,[毫不奇怪]看到这不是form_closing事件。我的问题是,CancelEventArgs与FormClosingArgs实际发生了什么区别,或者这两段代码实际上是在做相同的事情,其中一个是System的组件,另一个是Windows事件处理的结果最好的是什么?我只是在跳水并将自己作为实习生投入到这个新项目中。是否可以仅用FormClosing替换CancelEventArgs而不造成任何数据丢失或问题?
代码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;
}
}
}
最佳答案
您不会通过CancelEventArgs类获得CloseReason属性,这是唯一的区别,因为FormClosingEventArgs继承自CancelEventArgs类。 .Net 2.0中引入了FormClosingEventArgs。
另外,也可以不使用事件,而只需重写OnFormClosing方法。
protected override void OnFormClosing(FormClosingEventArgs e) {
// your code
base.OnFormClosing(e);
}
关于c# - 使用CancelEventArgs的Closing()与使用FormClosingEventArgs的Form_Closing,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25119082/