Click here to Skip to main content
15,891,912 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
frmSChild objfrmSChild = frmSChild.GetChildInstance();
objfrmSChild.MdiParent = this;
objfrmSChild.Show();
objfrmSChild.BringToFront();


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?
Posted
Updated 4-Jun-11 1:59am
v3
Comments
Ragi Gopi 4-Jun-11 7:06am    
error "Cannot access a disposed object. Object name: 'frmSChild..."

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:

C#
protected override void Dispose( bool disposing )
{
    if( disposing )
    {
        if(components != null)
        {
            components.Dispose();
        }
    }
    base.Dispose( disposing );
    m_SChildform = null;
}



Valery.
 
Share this answer
 
Comments
Ragi Gopi 6-Jun-11 2:45am    
@valery possoz
its works buddy....
thanks..........
That is because objfrmSChild Form is Closed[^].

When Close method is called. All resources created within the object are closed and the form is disposed.

What happens in GetChildInstance ?
 
Share this answer
 
Comments
Ragi Gopi 4-Jun-11 7:23am    
can u plz suggest me a solution for that....
Kim Togo 4-Jun-11 8:50am    
It is a bet hard to do that. I have to see what GetChildInstance method is doing. Can you press "Improve question" and tell us what GetChildInstance do ?
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.
 
Share this answer
 
Just in case anyone was wondering, this is the author's text in the article he is following, just to reveal the GetChildInstance() method for everyone... just meant to keep user from opening more than 1 of that form at a time.

OP, as others have said, your problem has to do with how the child form is closed and disposed, and how the frmSChild handles it's instance check below to be created for the parent... I would submit your issue on the article page to the author can help you remedy the logic.

Putting life in the menus created by adding code to it
--------------------------------------------------------
1> Now in the frmSChild code add the following lines

  private static frmSChild m_SChildform;
  public static frmSChild GetChildInstance()
  {
   if (m_SChildform ==null) //if not created yet, Create an instance
    m_SChildform = new frmSChild();
   return m_SChildform;  //just created or created earlier.Return it
  }
//This is to make sure that when we Click on a 'New Single' menu on Parent form twice 
//it should not open two instance of the same child form
 
Share this answer
 

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