如何退出与创建表单不同的类中的表单

时间:2014-01-05 05:44:37

标签: c# winforms

我有这个项目需要包含多个表单。问题是我的第一个表格不会关闭。我有两个班级,BetaForm1。我正在尝试使用此代码退出WinForm通过类Form1创建的Beta

Form1 Form1 = new Form1();
Form1.Close();

那不起作用,我去探索其他选择。我决定在按钮单击事件中添加表单的结束,我从Form1类转移到Beta类,但这也不起作用。它会完全关闭整个程序。这是我做的代码:

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close(); // Closes the program if I include this here
        Beta.ExecuteMainUIThread();
        this.Close(); // Unaccessible code
    }

我的问题是,在我尝试保持程序运行的同时,我将退出原始表单并启动另一个表单

1 个答案:

答案 0 :(得分:0)

不完全确定你的问题是什么,但是根据我的理解,你有一个父母和一个孩子的表格,你想要从父母那里打开/关闭孩子,也许你可以这样做:

(在家长中):

private ChildForm _childForm;

private void btnOpenChild_Click(object sender, EventArgs e)
{
    if (_childForm == null)
    {
        _childForm = new ChildForm();
        _childForm.Show(this);
    }
}

private void btnCloseChild_Click(object sender, EventArgs e)
{
    if (_childForm != null)
    {
        _childForm.Close();
        _childForm.Dispose();
        _childForm = null;
    }
}