2013-02-01 34 views
1

我有一个MDI form,其中一些子窗体需要在关闭之前显示一个消息框,而其他人可以在不询问的情况下关闭。由于儿童表格的close event致电application.Exit()时出现问题,我会处理父母的close event,并检查它是在哪里被解雇的。如果它是在需要消息框的表单中被触发的,我会调用它,否则只需关闭应用程序。所有这一切都是在这个代码实现:C#将long if语句转换为更合适的东西

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语句是错误的代码的迹象,但我不知道如何改进它。处理这种情况的正确方法是什么?

+3

使用CAST只核对空。使用is运算符 - 为您节省六行代码。 –

+0

@DasKrümelmonster我不明白你在说什么。你怎么能在这里使用'is'运算符? – Khan

+1

@JefferyKhan因为Leron实际上并没有对铸造结果做任何事情,通过使用'is'它只是返回true/false和IMO,更具可读性:'if(ActiveMdiChild是SEdit‖ActiveControl是SEdit‖ActiveControl是SoEdit || ...)'(检查可以/应该仍然移动到本地布尔值或最好是一个很好的命名方法,如下所示,但即使在那些我建议使用'is'仍然) –

回答

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

我会用你的解决方案,谢谢。 – Leron

+0

在这里不会使用'is'而不是使用'as' + null检查吗? –

+0

@ChrisSinclair我曾考虑过这个问题,但结果可能会有所不同,但它有一些等于null的内容,但却有必要的类型。 –

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; 
} 
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); 
    } 
    } 
} 

因为这是怎么写的,你就不会实现该接口为所有的形式,只是那些你真正想问的问题。

+0

确实同意,非常优雅的解决方案。 – Leron

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); 
      } 
     } 
    } 
0

不是最好的方式,但你可以的形式Tag属性设置为0或1,检查:

  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); 
       } 
      }