模态形式在关闭时会解散

时间:2013-07-25 15:45:55

标签: c# winforms modal-dialog messagebox

我有一个模态形式说form1。有一个按钮,当用户点击它时,另一个模态表单显示为formchild。如果有任何错误,我想解雇formchild并显示一个消息框。 我使用以下代码。但是,我看到的是一个消息框显示在formchild的顶部 如何让formchild消失/关闭?谢谢

        formchild.DialogResult = DialogResult.Cancel;
        formchild.Close();
        MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); 

2 个答案:

答案 0 :(得分:0)

为什么不在调用formchild之前输入“If”语句?

//some codes here:
if (!Error)
    formchild.ShowDialog();
else
    Messagebox.Show("Error has occured.");

或者如果那不可能,请尝试将该if语句放在formchild.Load。

      private void formchild_Load(object sender, EventArgs e)
  {
       //some codes here:
           if (Error)
           {                        
                Messagebox.Show("Error has occured.");
                this.Close();
           }
  }

答案 1 :(得分:0)

对于formchild,解决方案设置为false。感谢

formchild.Visible = false;
formchild.Close();
MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); 
相关问题