PDA

Click to See Complete Forum and Search --> : [1.0/1.1] Checking whether the form is disposed or not!


pvbangera
Jun 19th, 2007, 01:51 PM
On a mdi's menu click i am calling a child form.

On the click event I want to check whether the form is already initiated. if not then i want to initiate it first and then call the show method. on closing the form i dispose it.

How to chk whether the form is disposed or not?

IsNothing is not working

jmcilhinney
Jun 19th, 2007, 07:04 PM
I'm not sure whether you have resolved this or not but the code should look something like this:private Form2 childForm;

private void ShowChildForm()
{
if (this.childForm == null || this.childForm.IsDisposed)
{
// Create a new child form and show it.
this.childForm = new Form2();
this.childForm.MdiParent = this;
this.childForm.Show();
}
else
{
// Activate the existing child form.
this.childForm.Activate();
}
}Note that if there is not supposed to be more than one instance of a form then you can also have that form implement the Singleton pattern:public partial class Form2 : Form
{
private static Form2 _instance;

public static Form2 Instance
{
get
{
if (_instance == null || _instance.IsDisposed)
{
_instance = new Form2();
}

return _instance;
}
}

private Form2()
{
InitializeComponent();
}
}Then your code in the parent form becomes:private void ShowChildForm()
{
Form2.Instance.MdiParent = this;
Form2.Instance.Show();
Form2.Instance.Activate();
}