"Greg" <Gr**@discussions.microsoft.com> a écrit dans le message de news:
D0**********************************@microsoft.com...
I have a form that is created if it is not already created.
The code goes something like this:
if (MyForm == null)
MyForm = new MyForm();
else
MyForm.Activate
The problem is that if the form has been closed with the control box, the
form will not be null, but it will not activate either. So what is the
proper way to check for the form to see if it exists and is activatable?
This is a common problem and can be solved by the creation of a Singleton
style of form class.
e.g.
public class MyForm
{
private MyForm() : base() {} // hide regular constructor
private static Form2 instance = null;
public static void ShowForm()
{
if (instance == null)
instance = new Form2();
instance.Show();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
instance = null;
}
}
This is then called like this :
private void button1_Click(object sender, System.EventArgs e)
{
Form2.ShowForm();
}
Joanna
--
Joanna Carter
Consultant Software Engineer