471,319 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Checking to see if a form exists

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
Jan 3 '06 #1
5 8818
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;
}

}

"RSH" <wa*************@yahoo.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...

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

Jan 3 '06 #2
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
"Lebesgue" <no****@spam.jp> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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;
}

}

"RSH" <wa*************@yahoo.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...

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


Jan 3 '06 #3
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);

"RSH" <wa*************@yahoo.com> wrote in message
news:O7**************@TK2MSFTNGP11.phx.gbl...
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
"Lebesgue" <no****@spam.jp> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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;
}

}

"RSH" <wa*************@yahoo.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...

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



Jan 3 '06 #4
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.

Jan 3 '06 #5
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.
"RSH" <wa*************@yahoo.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...

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

Jan 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mike | last post: by
1 post views Thread by wg | last post: by
5 posts views Thread by BerkshireGuy | last post: by
15 posts views Thread by Geiregat Jonas | last post: by
9 posts views Thread by D. Shane Fowlkes | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.