Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Mini_dbms
{
    public partial class Form1 : Form
    {
        CreateDB.Form2 CR = new CreateDB.Form2();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            //CreateDB.Form2 CR = new CreateDB.Form2(); 
            CR.Show();    //  error in here  while reopenig it<< ObjcetDisposedException  was unhandled  "cannot  access  a disposed object." >> 
       
        }

        private void Main_Load(object sender, EventArgs e)
        {
            
            CR.FormClosed += new FormClosedEventHandler(CR_FormClosed);
           
        }

        void CR_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (CR.flag == 1)
            {
                toolStripStatusLabel1.Text = "successfully created!";
                
            }
            else if(CR.flag==2)
            {
                toolStripStatusLabel1.Text = "Creation failed !";
            }
            
        }
    }
}



when user clicks on this event <CreateToolStripMenuItem > ,CR shows the form ,but when you close this form and wanna
reopen it ,below error occurs
<< ObjcetDisposedException was unhandled "cannot access a disposed object." >> :(

where shall I get the instace of the object (CR) ?:confused:
pay attention that I used the FormClosedEventHandler .
this is my code
Posted
Updated 9-Feb-10 22:24pm
v2

The error means what it says. When your form is closed, you need to add code that hides it rather than disposes of it, to that form. The closing event needs to STOP the close, then just hide the form. Otherwise, when you try to show it again, it's been disposed of, and so you get exactly the error you're dealing with.
 
Share this answer
 
You got to becarefull stating that there is a Bug in C#, first if this is bug which is not, you should say a bug in the framework or in Form class.

Anyway this is not a bug, you are closing the form of which it disposes the CR object, now you still have the reference to the CR as you are declaring it in the scope of the main form, you call the CR.Show() method which will give you an exception since the CR is already disposed.
 
Share this answer
 
re answer 4 - It's FormClosing, not FormClosed that you need, it has a Cancel property in FormClosingEventArgs. So if you set Cancel to false, then the form will not be closed.
 
Share this answer
 
v3
You are losing the object when closing the form. I would suggest to cancel FormClosing events and just Hide() the form. So, in CR_FormClosing (not CR_FormClosed) you would have
e.Cancel = true;
CR.Hide();


and then in CreateToolStripMenuItem_Click you would have
if (CR == null) 
   CR = new CreateDB.Form2();
CR.Show();


This way you create the form only once, and then just hide/show it when necessary.
 
Share this answer
 
v2
Thanks for your advising guys , but Mr.Marcel in the CR_FormClosed I haven't (( e.cancel )) property ! :((
So come on good boys ! gimme a solution ! please don't explain the error for me (i can read English!! :laugh: ).
 
Share this answer
 
Hey DaveyM69 ,Thank you ... you're Hard ROCK! we love you boy.. :-D ;)
 
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