Click here to Skip to main content
15,569,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Now am having one form(Form A),having 2 buttons ie, OPEN and CLOSE. When am click button OPEN, my second form(Form B) is display. And again when am press the button CLOSE at Form A, My second form was closed. Till now no problem. And again am click OPEN button at Form A, following error occured,
"Cannot access a disposed object.Object name: 'Form B'.". How to overcome this problem... All replies are welcome. My coding is

C#
FormB  _form2 = new Form2();
        private void btn_open_Click(object sender, EventArgs e)
        {
          
            _form2.2how();

        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            _form2.Close();

        }




Thanks in Advance

Dhinesh Kumar.V
Posted

You could use form2.Hide() instead of form2.Close(). If you insist on using form2.Close() you would have to instantiate a new form2 object when you see that form2 is null. In the close button's click method you should then se form2 = null after having called the Close() method on it.

Regards,

— Manfred
[Modification]
MSDN[^] wrote:
When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
 
Share this answer
 
v3
C#
FormB  _form2;

private void btn_open_Click(object sender, EventArgs e)
{

   if(_form2==null || _form2.IsDisposed)
   {
      _form2= new Form2();

   }
   _form2.show();
}
 
Share this answer
 
v3
C#
public partial class form1 : Form
{
   
   public void btn_Open_Click(object sender, EventArgs e)
   {
     frm as _form2 = new _frm2()
     frm.show;
   }

   public void btn_Close_Click(object sender, EventArgs e)
   {
       frm as _form2 = new _frm2()
       frm.close
   }
}
 
Share this answer
 
C#
public partial class form2 : Form
{
   public void Exit()
   {
       this.Close();
   }
}


C#
public partial class form1 : Form
{
   form2 _form2 = new form2();

   public void btn_Open_Click(object sender, EventArgs e)
   {
       _form2.Show();
   }

   public void btn_Close_Click(object sender, EventArgs e)
   {
       _form2.Exit();
   }
}
 
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