如何关闭子表单和子表单的子表单?

时间:2014-12-12 23:00:28

标签: c# visual-studio-2013

为方便参考,我有一个父表格(1),它的'子表格(2),以及子表格的子表格(3)。

我进行了搜索和搜索,所有我发现的是在第3张打开时隐藏第2张表格的方法。我希望同时关闭第二和第三种形式,从第3种形式直接转到第1种形式。

在加载第3个表单时关闭第2个表单不是一个选项,因为我在第3个表单上也有一个函数this.Close();为了带我到第二个表格。

当我输入以下内容时:

 private void btn1_Click(object sender, EventArgs e)
    {
        this.Close();
        frm2.Close();
    }

我收到错误:

  

“非静态字段,方法或者需要对象引用   property'System.Windows.Forms.Form.Close()“

同样,我想要的只是一个命令,能够同时关闭两个表单,表单2和表单3。

编辑:

到目前为止我的(错误的)代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace frmGametime
{
public partial class frmX : Form
{
    public frmX()
    {
        private frmX frmTTT;
        public frmX()
        {
            InitializeComponent();
            frmTTT = new frmX();
            frmTTT.FormClosed += frmX_FormClosed;
        }

        void frmX_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        private void btnMM_Click(object sender, EventArgs e)
        {
            frmTTT.Show();
        }
    }

    private void frmX_Load(object sender, EventArgs e)
    {

    }

    private void btnNG_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btn1_Click(object sender, EventArgs e)
    {
        this.btn1.Text = "X";

        Random rng = new Random();
        int OppChoice = rng.Next(1, 9);

        if (OppChoice == 1)
        {
            if (this.btn1.Text == "_")
            {
                this.btn1.Text = "O";
            }
            else
            {
                OppChoice++;
            }
        }
        if (OppChoice == 2)

...

if (this.btn1.Text == "X" && this.btn2.Text == "X" && this.btn3.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn1.Text == "X" && this.btn4.Text == "X" && this.btn7.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn1.Text == "X" && this.btn5.Text == "X" && this.btn9.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn2.Text == "X" && this.btn5.Text == "X" && this.btn8.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn3.Text == "X" && this.btn6.Text == "X" && this.btn9.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn3.Text == "X" && this.btn5.Text == "X" && this.btn7.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn4.Text == "X" && this.btn5.Text == "X" && this.btn6.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }

        if (this.btn7.Text == "X" && this.btn8.Text == "X" && this.btn9.Text == "X")
        {
            this.lblWinner.Text = "X Wins!";
            this.lblWinner.Visible = true;
        }
    }

private void btnMM_Click(object sender, EventArgs e)
    {
        this.Close();    
    }


}
}

1 个答案:

答案 0 :(得分:1)

以第二种形式订阅第三张表格的已结束活动。当第三个表单关闭时,第二个表单将看到该事件并执行它的处理程序。在处理程序中,您可以执行此操作.Close();

 public partial class Form2 : Form
{
    private Form2 form2;
    public Form2()
    {
        InitializeComponent();
        form2 = new Form2();
        form2.FormClosed += form2_FormClosed;
    }

    void form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form2.Show();
    }
}  

更新了新示例的答案

首先,看起来你在构造函数中有一个构造函数。我甚至不确定这是如何编译的,但是根据它的外观,您的表单frmTTT在最后一行frmTTT.FormClosed += frmX_FormClosed;之后不再存在

如果您希望变量保持活跃状态​​,则必须将其保留在类frmX:form

的根目录中

更改

 public frmX()
{
    private frmX frmTTT;
    public frmX()
    {
        InitializeComponent();
        frmTTT = new frmX();
        frmTTT.FormClosed += frmX_FormClosed;
    }

    void frmX_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

    private void btnMM_Click(object sender, EventArgs e)
    {
        frmTTT.Show();
    }
}  

private frmX frmTTT;
    public frmX()
    {
        InitializeComponent();
        frmTTT = new frmX();
        frmTTT.FormClosed += frmX_FormClosed;
    }

    void frmX_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

    private void btnMM_Click(object sender, EventArgs e)
    {
        frmTTT.Show();
    }  

外部

 public frmX()
{

}  

不需要因为你已经有一个。这就是所谓的构造函数,它初始化您的对象(创建它并使其可以使用)。在WinForms中,您可以通过查找行InitializeComponent()轻松告诉哪个是表单构造函数。继续尝试。你不应该再得到那个空引用错误