如何在Windows应用程序中刷新表单对象 [英] how to refresh form object in windows application

查看:62
本文介绍了如何在Windows应用程序中刷新表单对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im使用Windows aaplication,我有2个Windows窗体,第一个是mdi父窗体,另一个是mdi child.parent窗体包含子窗体的链接.每当我运行我的父窗体并单击链接按钮,然后打开子页,然后我单击取消"按钮,然后关闭表单.我尝试再次打开子页面,但是当我单击舔"按钮时,我收到一条错误消息,例如无法访问已处置的对象.
对象名称:"obj"".

i m using windows aaplication and i have 2 windows form ,first is mdi parent and another is mdi child.parent form contain the link of child form .whenever i run my parent form and click link button then child page open ,and then i click cancel button then form get closed.i try to again open child page but when i click lick button then i m getting a error message like "Cannot access a disposed object.
Object name: ''obj''".

推荐答案

您无法打开(显示;没有打开")已关闭的表单,因为它已被丢弃在这里,最常见的模式是捕获关闭表单并取消它的尝试,然后将其隐藏,为此,您需要处理事件System.Windows.Forms.Form.FormClosing或重写虚拟方法System.Windows.Forms.Form.OnFormClosing.无论如何创建一个表单类,让我们考虑第二种方法:

You cannot open (show; there is no "open") a form which was closed, because it is disposed. The most usual pattern here is to capture the attempt to close a form and cancel it, hiding it instead. To do so, you would need to either handle the event System.Windows.Forms.Form.FormClosing or override the virtual method System.Windows.Forms.Form.OnFormClosing. As you usually create a form class anyway, let''s consider the second way:

public partial class MyForm // MyForm : System.Windows.Forms.Form shown in another part, usually auto-generated
{
    protected override OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == System.Windows.Forms.CloseReason.UserClosing) // only for this reason 
        {
            Hide();
            e.Cancel = true; // so it won't be actually closed
        }
    }

    //...
}



请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.formclosingeventargs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.closereason.aspx [ ^ ].

现在,关于MDI.这是一个主意:谁曾经需要MDI?为什么要折磨自己并吓users用户?
帮自己一个忙:完全不要使用MDI.没有设计,您可以更轻松地实现设计,并且质量更高.即使Microsoft也不鼓励使用MDI,实际上,Microsoft已将其从WPF中删除,并且几乎不支持它.更重要的是,如果您使用MDI,则会吓跑所有用户.只是不要.我可以解释该怎么做.

请参阅:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [如何在WPF中创建MDI父窗口? [ ^ ].

另请参阅我过去的答案:
在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何最大程度地设置子表单,最小化最后一个子表单 [ ^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^].

Now, about MDI. Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don''t. I can explain what to do instead.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

See also my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


这篇关于如何在Windows应用程序中刷新表单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆