从自定义消息框中关闭背景表单

时间:2014-02-04 15:22:09

标签: c# .net winforms visual-studio-2010

简要说明: 无法通过单击form2上的按钮来关闭form1。

我有以下表格:

form1 formMessageBox button1 form3 formMessageBox

我想: 从form1弹出formMessageBox(这可行),然后从button1点击form1关闭form3并打开form1

我使用了各种技术,但{{1}}无法定位。

非常感谢。

修改

当form1和消息框同时打开时。单击button1以尝试关闭form1时,Form1不会关闭或隐藏。

1 个答案:

答案 0 :(得分:0)

不确定你在寻找什么,但我认为你需要这样的东西。

假设您有Form1,并使用formMessageBox弹出ShowDialog()

public class Form1 : Form
{
    if (somethingHappened)
    {
        var formMessageBox = new FormMessageBox();

        //ShowDialog() sets the formMessageBox object as a modal dialog owned by Form1 
        DialogResult result = formMessageBox.ShowDialog();

        //Assuming you have OK buttons or something like that in formMessageBox
        if (result == DialogResult.OK)
        {
            var form3 = new Form3();

            /*Can't use this.Close() here as it would close the entire application
            Just hide it and don't forget to dispose of it later when you actually
            want the application to close*/
            this.Hide();

            form3.Show();


        }
    }
}

然后在您的formMessageBox代码中,根据发生的情况设置DialogResult

public class FromMessageBox : Form
{
    private void okButton_Click(object sender, EventArgs e)
    {
         //Do some stuff
         this.DialogResult = DialogResult.OK
         this.Close();
    }
 }