when I try to reopen a form that was closed from the X of the window I get the exception System.ObjectDisposedException
with the message "Cannot access the discarded object. Object name: 'frm_schedules'."
I understand that X calls the method Dispose()
, which releases resources, but I don't know to what extent it releases. I check that the form is not Null
when I call it, but it keeps throwing that exception.
Code of the form frm_horarios, is what I call:
public partial class frm_horarios : Form
{
private static Form instancia = null; //implemento el patron singleton para tener solo una instancia de este formulario
private frm_horarios()
{
InitializeComponent();
}
public static Form getFormularioHorario()
{
if(instancia ==null)
{
instancia = new frm_horarios();
return instancia;
}
else
{
return instancia;
}
}
}
Code of the button that calls the form frm_hours:
private void btn_horarios_Click(object sender, EventArgs e)
{
if(horario !=null)//la variable horario es del tipo Form y se encuentra declarada en el principio de la clase
{
horario.Show();
}
else
{
horario = frm_horarios.getFormularioHorario();
horario.Show();
}
}
You can prevent the form closure from
frm_horarios
actually disposing of the object. To do this, you can add an eventFormClosing
:Another way to do this would be to open the child form as a dialog , by changing just one line of your code: