在C#中处理父表单中的表单?

时间:2021-10-14 11:23:39

I have a form which will open a new form when one button (form1button) is clicked. And on the child form there will be another button 'form2button'. Now if I click this form2button the new form2 should be disposed. But because the form2 object is created here in form1 class method, I cannot dispose that object in form2 class method (fom2buttonclick). So I used static to get my work done as in the following psuedo code.

我有一个表单,当单击一个按钮(form1button)时将打开一个新表单。在子表单上会有另一个按钮'form2button'。现在,如果我单击此form2按钮,则应该处理新的form2。但是因为form2对象是在form1类方法中创建的,所以我不能在form2类方法中处理该对象(fom2buttonclick)。所以我使用static来完成我的工作,如下面的psuedo代码。

Form1:

class Form1 : Form
{
    static Form2 f2;

    public void Form1_buttonclick(object sender, EventArgs e)
    {
        f2 = new Form2();
    }

    public void Disposef2()
    {
        f2.Dispose();
    }
}

Form2:

class Form2 : Form
{
    public void Form2_buttonclick(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Disposef2();
    }
}

Is there any other better way to do it. Or C# design itself doesnot provide an alternative mechanism. I am new to C#.Please help me out..

有没有其他更好的方法来做到这一点。或者C#设计本身并不提供替代机制。我是C#的新手。请帮帮我..

Edit

I want to close (dispose explicitely) form2 object which is created in form1 class when button on form2 is clicked. This edit is to give some more clarity.

我想关闭(明确处置)form2对象,当单击form2上的按钮时,该对象在form1类中创建。此编辑旨在提供更清晰的信息。

5 个解决方案

#1


If the two forms doesn't have a parent-dialog type of relationship, you might just want to hook into the Disposed event on the subform to get notified when it closes.

如果两个表单没有父对话框类型的关系,您可能只想挂钩子窗体上的Disposed事件以在关闭时获得通知。

public partial class Form1 : Form
{
    private Form2 _Form2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_Form2 != null)
            _Form2.Dispose();

        _Form2 = new Form2();
        _Form2.Disposed += delegate
        {
            _Form2.Dispose();
            _Form2 = null;
        };
        _Form2.Show();
    }
}

Then all you have to do in Form2 is simply to close it:

那么你在Form2中所要做的就是关闭它:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }
}

#2


MSDN docs on disposing of forms:

关于处理表单的MSDN文档:

Dispose will be called automatically if the form is shown using the Show method. If another method such as ShowDialog is used, or the form is never shown at all, you must call Dispose yourself within your application.

如果使用Show方法显示表单,将自动调用Dispose。如果使用其他方法(如ShowDialog),或者根本不显示表单,则必须在应用程序中调用Dispose。

Source

On closing vs. disposing:

关闭与处置:

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

关闭表单时,将关闭对象中创建的所有资源并处理表单。您可以通过处理Closing事件并将作为参数传递的CancelEventArgs的Cancel属性设置为事件处理程序来阻止在运行时关闭表单。如果您要关闭的表单是应用程序的启动表单,则应用程序结束。

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

表单未在Close上处理时的两个条件是(1)它是多文档界面(MDI)应用程序的一部分,并且表单不可见; (2)您使用ShowDialog显示了表单。在这些情况下,您需要手动调用Dispose以标记所有表单的垃圾回收控件。

#3


You are not really a reader right? Lot of answers here already.

你真的不是读者吗?这里已有很多答案。

Edit I want to close(dispose explicitely) form2 object which is created in form1 class when button on form2 is clicked. This edit is to give some more clarity.

编辑我想关闭(显式处置)form2对象,当单击form2上的按钮时,该对象在form1类中创建。此编辑旨在提供更清晰的信息。

If you use ShowDialog then form2 returns when you call close(). So in Form1:

如果使用ShowDialog,则在调用close()时返回form2。所以在Form1中:

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 oForm2 = new Form2();
    oForm2.MyParentForm = this;
    if (oForm2.ShowDialog() == DialogResult.OK)
    {
        oForm2.Dispose(); //or oForm2.Close() what you want
    }
}

And then call Close() in form2.

然后在form2中调用Close()。

#4


  1. You don't need to explicitly call Dispose on the form, the garbage collector will do that for you.

    您不需要在表单上显式调用Dispose,垃圾收集器将为您执行此操作。

  2. If you want something specific to happen when Form2 "goes away", you can hook into it's form closing event.

    如果你想在Form2“消失”时发生特定的事情,你可以勾选它的形式结束事件。

EDIT :

On Form2, in the button click, try

在Form2上,在按钮单击中,尝试

this->Close();

That will close that instance of form2 (the form will disappear). If form1 still has a reference to form2, then form2 will not be picked up by the garbage collector, and the GC will not dispose of it.

这将关闭form2的实例(表单将消失)。如果form1仍然具有对form2的引用,那么垃圾收集器将不会获取form2,并且GC将不会丢弃它。

If there is a reason for form1 to keep a reference to form2 ?

如果form1有理由保留对form2的引用?

If so, form1 should handle from2's closing event, then form1 can release it's reference to form2 (set it to null).

如果是这样,form1应该处理from2的结束事件,然后form1可以释放它对form2的引用(将其设置为null)。

Now the GC will pickup form2 as a candidate to be collected, it will (in possibly more than one step) call it's Dispose method and free up Form2's memory.

现在GC将拾取form2作为要收集的候选者,它将(可能不止一步)调用它的Dispose方法并释放Form2的内存。

#5


I did this once for my project, to close one application and open another application.

我为我的项目做过一次,关闭一个应用程序并打开另一个应用程序。

    System.Threading.Thread newThread;
    Form1 frmNewForm = new Form1;

   newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
        newThread.SetApartmentState(System.Threading.ApartmentState.STA);
        newThread.Start();

And add the following Method. Your newThread.Start will call this method.

并添加以下方法。你的newThread.Start会调用这个方法。

    public void frmNewFormThread)()
    {

        Application.Run(frmNewForm);

    }

#1


If the two forms doesn't have a parent-dialog type of relationship, you might just want to hook into the Disposed event on the subform to get notified when it closes.

如果两个表单没有父对话框类型的关系,您可能只想挂钩子窗体上的Disposed事件以在关闭时获得通知。

public partial class Form1 : Form
{
    private Form2 _Form2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_Form2 != null)
            _Form2.Dispose();

        _Form2 = new Form2();
        _Form2.Disposed += delegate
        {
            _Form2.Dispose();
            _Form2 = null;
        };
        _Form2.Show();
    }
}

Then all you have to do in Form2 is simply to close it:

那么你在Form2中所要做的就是关闭它:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }
}

#2


MSDN docs on disposing of forms:

关于处理表单的MSDN文档:

Dispose will be called automatically if the form is shown using the Show method. If another method such as ShowDialog is used, or the form is never shown at all, you must call Dispose yourself within your application.

如果使用Show方法显示表单,将自动调用Dispose。如果使用其他方法(如ShowDialog),或者根本不显示表单,则必须在应用程序中调用Dispose。

Source

On closing vs. disposing:

关闭与处置:

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

关闭表单时,将关闭对象中创建的所有资源并处理表单。您可以通过处理Closing事件并将作为参数传递的CancelEventArgs的Cancel属性设置为事件处理程序来阻止在运行时关闭表单。如果您要关闭的表单是应用程序的启动表单,则应用程序结束。

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

表单未在Close上处理时的两个条件是(1)它是多文档界面(MDI)应用程序的一部分,并且表单不可见; (2)您使用ShowDialog显示了表单。在这些情况下,您需要手动调用Dispose以标记所有表单的垃圾回收控件。

#3


You are not really a reader right? Lot of answers here already.

你真的不是读者吗?这里已有很多答案。

Edit I want to close(dispose explicitely) form2 object which is created in form1 class when button on form2 is clicked. This edit is to give some more clarity.

编辑我想关闭(显式处置)form2对象,当单击form2上的按钮时,该对象在form1类中创建。此编辑旨在提供更清晰的信息。

If you use ShowDialog then form2 returns when you call close(). So in Form1:

如果使用ShowDialog,则在调用close()时返回form2。所以在Form1中:

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 oForm2 = new Form2();
    oForm2.MyParentForm = this;
    if (oForm2.ShowDialog() == DialogResult.OK)
    {
        oForm2.Dispose(); //or oForm2.Close() what you want
    }
}

And then call Close() in form2.

然后在form2中调用Close()。

#4


  1. You don't need to explicitly call Dispose on the form, the garbage collector will do that for you.

    您不需要在表单上显式调用Dispose,垃圾收集器将为您执行此操作。

  2. If you want something specific to happen when Form2 "goes away", you can hook into it's form closing event.

    如果你想在Form2“消失”时发生特定的事情,你可以勾选它的形式结束事件。

EDIT :

On Form2, in the button click, try

在Form2上,在按钮单击中,尝试

this->Close();

That will close that instance of form2 (the form will disappear). If form1 still has a reference to form2, then form2 will not be picked up by the garbage collector, and the GC will not dispose of it.

这将关闭form2的实例(表单将消失)。如果form1仍然具有对form2的引用,那么垃圾收集器将不会获取form2,并且GC将不会丢弃它。

If there is a reason for form1 to keep a reference to form2 ?

如果form1有理由保留对form2的引用?

If so, form1 should handle from2's closing event, then form1 can release it's reference to form2 (set it to null).

如果是这样,form1应该处理from2的结束事件,然后form1可以释放它对form2的引用(将其设置为null)。

Now the GC will pickup form2 as a candidate to be collected, it will (in possibly more than one step) call it's Dispose method and free up Form2's memory.

现在GC将拾取form2作为要收集的候选者,它将(可能不止一步)调用它的Dispose方法并释放Form2的内存。

#5


I did this once for my project, to close one application and open another application.

我为我的项目做过一次,关闭一个应用程序并打开另一个应用程序。

    System.Threading.Thread newThread;
    Form1 frmNewForm = new Form1;

   newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
        newThread.SetApartmentState(System.Threading.ApartmentState.STA);
        newThread.Start();

And add the following Method. Your newThread.Start will call this method.

并添加以下方法。你的newThread.Start会调用这个方法。

    public void frmNewFormThread)()
    {

        Application.Run(frmNewForm);

    }