我正在尝试在打开新表单时关闭一个表单。
当关闭一个表单时,我希望在关闭事件中处理一些特殊的逻辑。
但是关闭事件从未被调用,在FormClosing和Closing事件中,也没有在抽象基类或给定的手动附加事件form_FormClosing中出现。
当我手动通过点击x关闭表单时,所有事件都会正常触发。但调用Close()方法失败了。
您有什么建议来解决我的问题吗?
MdiParent:
当我手动通过点击x关闭表单时,所有事件都会正常触发。但调用Close()方法失败了。
您有什么建议来解决我的问题吗?
MdiParent:
private Form _currentForm;
private void ShowForm<T>() where T : Form
{
if (_currentForm != null && !_currentForm.IsDisposed)
{
_currentForm.Hide();
_currentForm.Close();
}
var form = MdiChildren.FirstOrDefault(f => f.GetType() == typeof(T));
if (form == null)
{
form = _formFactory.CreateForm<T>();
form.MdiParent = this;
form.WindowState = FormWindowState.Maximized;
form.FormClosing += form_FormClosing;
_currentForm = form;
MdiBackground.Hide();
form.Show();
}
else
{
ActivateMdiChild(form);
form.Activate();
}
}
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// will not be called
}
抽象通用 mdi 子窗体:
public abstract partial class BaseForm<TEntity> : Form where TEntity : class, IEntity
{
protected override void OnClosing(CancelEventArgs e)
{
// wil not be called
if (EditMode == EditModes.Editable)
{
MessageBox.Show(this, "Please commit or abort your changes");
e.Cancel = true;
}
base.OnClosing(e);
}
}