Checking to see if a form exists

R

RSH

I am using code to create a new modal form from a parent form. It is not an
MDI form. I need to know if an instance of the form already exists so when
the user clicks on the button I can close any previous version of the form.
Here is the code I am using to create the form:

frmFindReplace frmFind = new frmFindReplace();

frmFind.Show();

frmFind.PrepareForm(this.dataGridView1);





Thanks!

Ron
 
L

Lebesgue

The easiest solution would be to include a static variable in class frmFind
(FindForm would be a better name ;-) ) which indicates if the form was
already instantiated. Then read this property or implement a static method
to show the form and eventually disallow instantiating it direcly by making
the constructor private.

class FindForm : Form
{
private static bool isShown;
public static bool IsShown
{
get { return isShown; }
}

private FindForm()
{
}

public static FindForm CreateAndShow()
{
FindForm res = new FindForm();
res.Show();
//eventually handle the closed event if you want to be absolutely
correct and precise
isShown = true;
return res;
}

}
 
R

RSH

I realized after posting this that there are a couple more crucial elements
involved.

I have a parent form that basically has two buttons. Each button spawns a
different child form. Basically I need to make sure that only one child
form can be spawned at one time and when a child form is displayed the
parent form is hidden, but when a child form is closed I need the parent
form to be shown again.

The problem I am running into is how to reference the parent form from the
children when I want to unhide it, and how to find out if a child form of
either type exists and close it before spawning a new instance.

Thanks for any help you might be able to give me,
Ron
 
L

Lebesgue

You can hook onto the Closed event of the child form from the parent

private void ShowChild()
{
ChildForm f = ChildForm.Show(); //see me previous post, this is the
"close existing form before spawning new instance"
f.Closed += new EventHandler(ChildClosed);
this.Visible = false;
}

private void ChildClosed(object sender, EventArgs e)
{
this.Visible = true;
}

if we want to speak about "how to pass the reference of the parent to the
child form, the solution is simple: pass it as argument to constructor (and
modify the constructor, of course)

ChildForm f = new ChildForm(this);
 
B

Bruce Wood

In the case of the child forms, I suggest that what you want is not to
"close any existing child form in order to open the new one," but
rather "if there is an existing child form open, show it, otherwise
create one."

The latter is the Singleton pattern, which is easily applied to Forms,
something like this:

public class ChildForm
{
private static ChildForm _instance = null;

private ChildForm()
{
... do usual constructor stuff... note that constructor is
"private" ...
}

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

This way, whenever you want (the one and only) child form, you just
say:

ChildForm.Instance

If there is no child form, then it will create one. If there already is
one, it will give it back to you. So, in your parent form code, you can
just say:

ChildForm.Instance.Show()

to show (or create and then show) the child form.
 
R

RSH

Thanks!

There is some really good stuff here. I see that forms really are nothing
but objects...for some reason I was having a tough time with that paradigm.
 

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