Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am making desktop application using C#.NET and SQL SERVER
i have made mdi parent form in which there are 3 forms
but the form openes once only.when i again open the form i get the message
cannot access disposed object

1st form
C#
private void mediaMethodOfPublicityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Media_Method_of_Publicity objmedia = Media_Method_of_Publicity.MIDI();
            objmedia.MdiParent = this;
            objmedia.Show();
            objmedia.BringToFront();
        }

2nd form
C#
private void publicityCampaignToolStripMenuItem_Click(object sender, EventArgs e)
{
    Publicity_Campaign publicity =  Publicity_Campaign.MIDI();
    publicity.MdiParent = this;
    publicity.Show();
    publicity.BringToFront();
}


I have made two classes
clsglobal.cs and coding is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Integrates_System_PSM
{
   public sealed class clsglobal
    {
       public static frmMDI MDI;
    }
}


clsmain.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Integrates_System_PSM
{
    class ClSmain
    {
        ClSmain()
        {
        }

    }
}

and a function MIDI creted
C#
private static Media_Method_of_Publicity media;
        public static Media_Method_of_Publicity MIDI()
        {
            if (media == null) //if not created yet, Create an instance
                media = new Media_Method_of_Publicity();
            return media; //just created or created earlier.Return it
            //This is to make sure that when we Click on a 'Media_Method_of_Publicity' menu on Parent form twice
            //it should not open two instance of the same child form
        }

same for other 3 forms
Posted
Updated 2-May-11 1:17am
v2
Comments
Ankit Rajput 2-May-11 7:03am    
Provide the code from where you are opening the Forms.
Ankit Rajput 2-May-11 7:29am    
I have updated my answer please try it.

Consider alternative: not using MDI. This UI style is extremely inconvenient and is strongly discourages, even by Microsoft. There is no professional software using this style. One of the simplest yet robust alternatives is the tabbed interface based on System.Windows.Forms.TabControl.

For some more details here:
works with many win form environment[^] (see my solution).

—SA
 
Share this answer
 
You can bypass this problem by hiding the mdi child form instead of closing the mdi child.

catch the Form_Closing event and set de event.Cancel property as true. This will prevent the mdi child from closing. Then you hide the mdi child.

When you want to reopen the mdi child, search the childs of the mdi parent to check if the form allready exist. If so, just unhide the mdi child, otherwise create a new instance of the mdi child.
 
Share this answer
 
Comments
Ankit Rajput 2-May-11 7:14am    
One thing, I want to know, as you are suggesting to hide the form. Will it not create the performance or memory issues?
Wannes Geysen 2-May-11 7:17am    
that's true. If memory is an issue, you can always check if the mdi child != null and if so, create a new instance of the mdi child. That way, you don't have to catch the close event.
yesotaso 2-May-11 7:31am    
My 5 + to OP:
Checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
It is a matter of accessing memory no longer yours after Close() event. That is your 1st concern then you decide whether you want to keep Form hidden or recreate it.
Sergey Alexandrovich Kryukov 2-May-11 8:45am    
There is not such event "Form_Closing". Other then that, it's a right way. The event is FormClosing, or the virtual method OnFormClosing could be overridden. So, my 5.
--SA
Sergey Alexandrovich Kryukov 2-May-11 8:51am    
Please see my alternative solution.
--SA
hi,

Problem is occuring because you are trying to access a object which is already disposed.

It might be the problem when you are initializing the Form object.

C#
public static Media_Method_of_Publicity MIDI()
        {
            if (media == null) //if not created yet, Create an instance
            {
                media = new Media_Method_of_Publicity();
                media.Disposed+=new EventHandler(media_Disposed);
            }
            return media; 
        }

static void media_Disposed(object sender, EventArgs e)
        {
            media = null;
        }


Just MIDI function like this and Add this event handler code into your class.


Regards
Ankit
 
Share this answer
 
v2
Comments
shivani 2013 2-May-11 7:37am    
thanks
Ankit Rajput 2-May-11 7:42am    
most welcome.

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