从 C# 中的父表单处理表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disposing a form from parent form in C#?
提问by Enjoy coding
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”。现在,如果我点击这个 form2button,新的 form2 应该被处理掉。但是因为 form2 对象是在 form1 类方法中创建的,所以我无法在 form2 类方法 (fom2buttonclick) 中处理该对象。所以我使用静态来完成我的工作,如下面的伪代码所示。
Form1:
表格1:
class Form1 : Form
{
static Form2 f2;
public void Form1_buttonclick(object sender, EventArgs e)
{
f2 = new Form2();
}
public void Disposef2()
{
f2.Dispose();
}
}
Form2:
表格2:
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 类中创建的。此编辑是为了更清晰。
采纳答案by Lasse V. Karlsen
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();
}
}
回答by Binary Worrier
You don't need to explicitly call Dispose on the form, the garbage collector will do that for you.
If you want something specific to happen when Form2 "goes away", you can hook into it's form closing event.
您不需要在表单上显式调用 Dispose,垃圾收集器会为您执行此操作。
如果您希望在 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 的内存。
回答by Igor Brejc
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。
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.
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.
关闭表单时,对象内创建的所有资源都将关闭,并处理该表单。您可以通过处理 Closing 事件并设置作为参数传递给事件处理程序的 CancelEventArgs 的 Cancel 属性来防止在运行时关闭表单。如果您关闭的表单是您的应用程序的启动表单,您的应用程序将结束。
表单未在 Close 上处理的两个条件是:(1) 它是多文档界面 (MDI) 应用程序的一部分,并且该表单不可见;(2) 您已经使用 ShowDialog 显示了表单。在这些情况下,您需要手动调用 Dispose 来标记所有表单控件进行垃圾回收。
回答by RvdK
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()。
回答by franklins
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);
}