我有一个MDI form
,其中一些儿童表格需要在关闭前显示一个消息框,其他人可以在不询问的情况下关闭。由于从application.Exit()
子表单调用close event
时遇到问题,我处理父表的close event
并检查它被触发的位置。如果它是以需要消息框的形式触发的,我会调用它,否则只需关闭应用程序。所有这些都在此代码中实现:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
SEdit se = this.ActiveMdiChild as SEdit;
SoEdit soleEdit = this.ActiveControl as SoEdit;
UppEdit ue = this.ActiveControl as UpEdit;
MEdit mat = this.ActiveControl as MEdit;
LEdit lse = this.ActiveControl as LEdit;
CEdit cle = this.ActiveControl as CEdit;
if (se != null || soleEdit != null || ue != null || mat != null || lse != null || cle != null)
{
if (MessageBox.Show("Do you want to save before exit?", "Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("To Do saved.", "Status",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
我还在学习,但我知道这么长的if语句是代码错误的标志,但我不知道如何改进它。处理这种情况的正确方法是什么?
答案 0 :(得分:6)
提取条件以分离方法:
private bool AnyChildAlive()
{
return (this.ActiveMdiChild is SEdit) ||
(this.ActiveControl is SoEdit) ||
...
(this.ActiveControl is CEdit);
}
然后调用此方法(也使用保护条件来避免嵌套的if语句):
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!AnyChildAlive())
return;
if (MessageBox.Show("Do you want to save before exit?", "Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) != DialogResult.Yes)
return;
MessageBox.Show("To Do saved.", "Status",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
答案 1 :(得分:2)
可能最好的方法是创建一个类似的接口:
public interface IFormActions {
bool AskBeforeClosing();
void SaveData();
}
然后,为每个表单实现该接口,并在MainForm_FormClosing方法中执行此操作:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
IFormActions se = this.ActiveControl as IFormActions;
if ((se != null) && se.AskBeforeClosing()) {
if (MessageBox.Show("Do you want to save before exit?", "Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) {
se.SaveData();
MessageBox.Show("Saved", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
由于这是如何写的,你不必为所有表单实现接口,只需要实际想要提出结束问题的接口。
答案 2 :(得分:1)
为了使其更具吸引力或重复使用,您可能需要考虑将验证转移到其他方法。
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FormIsValid())
{
if (MessageBox.Show("Do you want to save before exit?", "Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("To Do saved.", "Status",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
private bool FormIsValid()
{
return
(this.ActiveMdiChild as SEdit) != null ||
(this.ActiveControl as SoEdit) != null ||
(this.ActiveControl as UpEdit) != null ||
(this.ActiveControl as MEdit) != null ||
(this.ActiveControl as LEdit) != null ||
(this.ActiveControl as CEdit) != null;
}
答案 3 :(得分:0)
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
var objects = new List<object>
{
this.ActiveMdiChild as SEdit,
this.ActiveControl as SoEdit,
this.ActiveControl as UpEdit,
this.ActiveControl as LEdit,
this.ActiveControl as CEdit
};
if (objects.Any(x => x != null))
{
if (MessageBox.Show("Do you want to save before exit?", "Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("To Do saved.", "Status",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
答案 4 :(得分:0)
不是最好的方法,但您可以将表单的Tag
属性设置为1或0并检查:
if (this.ActiveControl.Tag == 1)
{
if (MessageBox.Show("Do you want to save before exit?", "Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("To Do saved.", "Status",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}