单实例子窗体不起作用 [英] Single instance Child form not working
本文介绍了单实例子窗体不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
frmSChild objfrmSChild = frmSChild.GetChildInstance();
objfrmSChild.MdiParent = this;
objfrmSChild.Show();
objfrmSChild.BringToFront();
一切正常.但是它只能工作一次.
我打开那个frmSChild 2或3次.它将提出该表格.
但是,如果我关闭该表格并再次打开.出现错误,指出无法访问已处置的对象.对象名称:"frmSChild ..."
It is working fine. But it only works once.
I open the that frmSChild for 2 or 3 times. it will bring forwards that form.
But if I close that form and open again. there is an error stating "Cannot access a disposed object. Object name: ''frmSChild..."
How to modify the code to get it done?
推荐答案
你好,
这是一篇很好的文章,显示了如何为MDI表单实现单例模式:
用于MDI子表单的单一模式 [
hello,
here is a good article showing how to implement the singleton pattern for MDI forms:
Singleton pattern for MDI child forms[^]
one of the important bit is how to dispose of the form:
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
m_SChildform = null;
}
Valery.
Valery.
这是因为objfrmSChild表单是 ^ ].
调用Close
方法时.关闭在对象内创建的所有资源,并处置表单.
GetChildInstance
中会发生什么?
That is because objfrmSChild Form is Closed[^].
WhenClose
method is called. All resources created within the object are closed and the form is disposed.
What happens inGetChildInstance
?
我已经回答了这个问题 ^ ],包括说明您处置的对象不再存在的原因.
考虑到您的一些评论,您似乎一直在遵循一些逐步指南,但并没有真正理解代码中正在发生的事情.我建议您在处置 [
I have alread answered this question here[^], including explaining why your disposed object no longer exists.
Given some of your comments it appears that you have been following some step by step guide without really understanding what is going on in the code. I suggest you take a look at the MSDN documentation on disposing[^].
Also, please do not start a new thread with exactly the same question but add some more detail to the original.
这篇关于单实例子窗体不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文