473,543 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Prepare Form(this.dataG ridView1);

Thanks!

Ron
Jan 3 '06 #1
5 8963
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******** ******@TK2MSFTN GP14.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.Prepare Form(this.dataG ridView1);

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******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP14.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.Prepare Form(this.dataG ridView1);

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(Ch ildClosed);
this.Visible = false;
}

private void ChildClosed(obj ect 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******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP14.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.Prepare Form(this.dataG ridView1);

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._ins tance == null)
{
ChildForm._inst ance = new ChildForm();
}
return ChildForm._inst ance;
}
}
}

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

ChildForm.Insta nce

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.Insta nce.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******** ******@TK2MSFTN GP14.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.Prepare Form(this.dataG ridView1);

Thanks!

Ron

Jan 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2021
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data" onSubmit="return(validateData(this))"> <input type="file" name="txtFileToUpload"> <input type="submit" name="btnAdd" value="Add" class="form_button"> </form> ...
1
2234
by: wg | last post by:
Hey, Can someone show my how to change the code below to check to see if a variable exists rather than if the variable contains an empty string. if (document.form.regionselection.value == "") { missinginfo += "\n - Region";
5
23037
by: BerkshireGuy | last post by:
Hello everyone, I have a bond form that a user uses to enter data. One of my fields, is PolicyNumber. I added some code on the Before Update event of txtPolicyNumber that checks to see if that policy number is in the system when entering a new record. If it is, I want it to display a message and go back to the policy number field. ...
15
114179
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
9
5602
by: D. Shane Fowlkes | last post by:
(ASP.NET 2 / VB) Question - How can I write a If statement to see if a control (textbox) actually exists on a page? Upon page_load, a certain control may or may not be visible on the page so I need to first check to see if it actually exists and then if it does, get the value from it. If I try to read it or check if its "visible" if it's...
8
17638
by: Brad Pears | last post by:
I want to check to see if a particular object exists using vb.net 2005. I have a class called clsContract. When the user clicks the 'New Contract' button, I want to check to see if a clsContract object already exists and if so what the state of that object is before doing anything else. I thought I could use ... if...
1
1996
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i even tried with echo also how to solve this the message should come under in the same window only see my code it is program ---- <?php //...
6
28879
tuxalot
by: tuxalot | last post by:
I've tested many ways to accomplish this, but none of the code I've found works quite right. I am using the code written by Dev Ashish but it replicates some of the functionality I already have. And, if you change the path to the back end manually and launch the db, Dev's code throws an error. I think this request is fairly straightforward...
1
1728
by: Orbie | last post by:
Hi All, I'm new to VB.NET and i'm looking for some help with my Windows Form. I need to check if a Commodity entered into (TextBox1.Text) already exists on my table before i insert it. I'm having issues checking if the number of rows returned from my Select is equal 0? Also i'm wondering should i be checking for an exception and open/closing my...
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7735
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.