exist child form?

R

Realite

Hi,
I want to use child form,where i'm in mdi form i push the button and
the child form is showing,and i push again and the child form is
showing again and again and again.
How am i solve this?
Thank's...Realite
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

Well it's as simple as keeping a reference to the child form, when the
button ins clicked you check if the form exist , if so do not create it
again.

When the form is closed it should update the reference hold in the parent
form and set it to null
 
B

Bruce Wood

Make the child form a Singleton. "Singleton" is a common design pattern
that you can read about in many places. Here is a rough sketch:

public ChildForm : Form
{
private static ChildForm _instance = null;

private ChildForm()
{ ... the usual constructor stuff ... }

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

Now, whenever you want to so something with the child form, say
ChildForm.Instance. For example:

private void button1_Click(object sender, System.EventArgs e)
{
ChildForm.Instance.Show();
ChildForm.Instance.Focus();
}

This will show the child form if it is already showing, or create one
if it doesn't exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top