The PicForm
does not know anything about your variable here. You have to set variable to null (e.x. on FormClosed event).
public PicForm getPicForm()
{
if (dspForm == null)
{
dspForm = new PicForm();
dspForm.FormClosed += PicForm_FormClosed;
dspForm.Visible = true;
}
dspForm.Show();
return dspForm;
}
private void PicForm_FormClosed(object sender, FormClosedEventArgs e)
{
PicForm picForm =(PicForm)sender;
picForm.FormClosed += PicForm_FormClosed;
dspForm = null;
}
If you do not want to use additional events you could check if form was disposed (it will be if you close it ant it was shown with Show() method)
public PicForm getPicForm()
{
if (dspForm == null || dspForm.Disposing || dspForm.IsDisposed)
{
dspForm = new PicForm();
dspForm.Visible = true;
}
dspForm.Show();
return dspForm;
}
With this solution if you use dspForm
somewhere else you also need to check if it was disposed. Otherwise you can get ObjectDisposedException