Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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'".
Posted
Comments
Shambhoo kumar 17-Nov-12 22:21pm    
Any one help me.....
Sergey Alexandrovich Kryukov 17-Nov-12 22:24pm    
Any particular reason to use MDI?
--SA
Shambhoo kumar 17-Nov-12 22:32pm    
not any particular reason just i try to do it but i m getting problem...so help me.
Sergey Alexandrovich Kryukov 17-Nov-12 22:38pm    
I cannot see any valid reason to use such thing. -- I advised what to do instead in my answer (it's in the links to my past answers).

I also explained how the problem with disposed forms is solved.
--SA

1 solution

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:

C#
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
        }
    }

    //...
}


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
 
Share this answer
 
Comments
Shambhoo kumar 17-Nov-12 22:46pm    
Thanks alot sir.....
:)
:):):)
Sergey Alexandrovich Kryukov 17-Nov-12 22:51pm    
My pleasure.
Good luck, call again.
--SA
Shambhoo kumar 17-Nov-12 23:05pm    
ya sir...
i wanna add to u in my facebook frnd so plz sent me your Facebook id.
Sergey Alexandrovich Kryukov 17-Nov-12 23:15pm    
Do I look like the one who would use Facebook? :-)
--SA
Shambhoo kumar 20-Nov-12 1:00am    
But Why Not u use it...?
any reason....?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900