且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#错误(已处置对象)

更新时间:2023-12-04 20:48:46

错误表示它的意思.关闭表单后,您需要向该表单添加隐藏该表单而不是对其进行处理的代码.关闭事件需要停止关闭,然后才隐藏表单.否则,当您再次尝试显示它时,它已经被处理掉了,因此您可以准确地得到正在处理的错误.


您必须格外小心地声明在那里是C#中的错误,首先,如果这不是错误,则应在框架或Form类中说一个错误.

无论如何,这都不是一个错误,您要关闭它放置CR对象的形式,现在您仍具有对CR的引用,因为您在主表单的范围内对其进行了声明,请调用CR.Show() 答案4-因为您需要的是FormClosing而不是FormClosed,所以它具有FormClosingEventArgs中的属性.因此,如果将Cancel设置为false,则不会关闭该窗体.


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

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.


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.


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.