Hello

I've made tho following function in a form to avoid to open severall instance a child form dialog
It works fine but I do'nt know how to check if the user ave close the form from itself!
Because in such situation FrmZTour remain not null and I got an error !

// **************************************************************************************************
private void ShowForm(DataTable dtDagChauff)
{
if (FrmZTour == null )
{
FrmZTour = new FrmDgvZoomTour();
FrmZTour.SelectTour += new FrmDgvZoomTour.SelectTourHandler(DgvTourSelected);
FrmZTour.Show(this);
}

FrmZTour.FillDgv(dtDagChauff);
}
**************************************************************************************************
private void ShowForm(DataTable dtDagChauff)
{
if (FrmZTour == null )
{
FrmZTour = new FrmDgvZoomTour();
FrmZTour.SelectTour += new FrmDgvZoomTour.SelectTourHandler(DgvTourSelected);
FrmZTour.FormClosed += new FormClosedEventHandler(FrmZTour_FormClosed);
FrmZTour.Show(this);
}

FrmZTour.FillDgv(dtDagChauff);
}

private void FrmZTour_FormClosed(object sender, FormClosedEventArgs e)
{
     FrmZTour.Dispose();
     FrmZTour = null;
}

You need to use IsDisposed

if (m_FormProducts.IsDisposed)
       {
           m_FormProducts = new FormProducts();
       }

Be aware, that calling IsDisposed on an object that has not been initialized (IOW is null) will throw an Exception.
Also, IsDisposed is not set until Disposing is done. That is why it is a good idea to check the object for null before using or initializing it, then when done using it Dispose() of it (which marks it for garbage collection), then set the instance to null which just releases the local variable for reuse, the GC will still remove the memory footprint of the original instance address.

...or

you can use .ShowDialog() function, then one of the buttons in the dialog form will need the DialogResult property set, so that way you can catch DialogResult OK, or CANCEL or whatever... I think this is safer, but of course user can only open one dialog at time.

This article has been dead for over six months. Start a new discussion instead.