473,486 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to tell if an object exists

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?

--
Greg Gum
Nov 17 '05 #1
3 14830
Greg wrote:
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?

Add an eventhandler to the close event of your form and set your
instance to null in this.

MyForm.Closed += new EventHandler(Myfor_Closed);

private void MyForm_Closed(object sender, eventargs e)
{
myform = null;
}

Then it will work properly.
The problem is that you contain a reference (still valid) to a disposed
form which will obviously not allow you to work with it.

Cheers
JB
Nov 17 '05 #2
"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
Nov 17 '05 #3
you can also use Form.FindForm to find an already opened window.
"Greg" <Gr**@discussions.microsoft.com> schrieb im Newsbeitrag
news:D0**********************************@microsof t.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?

--
Greg Gum

Nov 17 '05 #4

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

Similar topics

7
12869
by: JT | last post by:
is there a way to determine if a form object actually exists? if i use the following syntax: varTextField = Request.Form("txtFormField") then varTextField = "" which gives the same result...
23
25593
by: Randell D. | last post by:
Folks, I have written some scripting tools that are compatable with alot of my pages - For example, I've created a script that will check to ensure certain form fields that require data, are...
3
13633
by: Gareth Tonnessen | last post by:
I need to have a clean way to determine whether a query exists without trying to open it and getting an error message. Is there a simple way to determine whether an object exists in a database?...
1
7741
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
6
15423
by: Darren Linsley | last post by:
I know this might seem like a dumb question, but how do you test that an object exists. Maybe i should explain a little. I have a variable that points to an object. Now if that oject is...
9
220284
by: Chris Ashley | last post by:
How do I check if an object exists in Javascript? EG: if (document.getElementById("product").value == null) { prodValue = ""; } else {
2
2627
by: tshad | last post by:
I have an object that may or may not exist on a page. Therefore, I test for it. But it doesn't seem to work if I do the following: if not HomeLink is nothing then...
4
1400
by: aaragon | last post by:
Hello everyone, I have this problem that is driving me crazy! I have a typedef for a singleton object in a file "fileA.h", then I try to use that object in "fileB.h" and then the compiler says...
4
7653
by: rickbird | last post by:
I have a composition object that is written in C++. The container class deletes the dynamic object in its destructor. However, if someone creates the object in the main and passes it to the...
0
7094
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,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6839
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...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4863
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...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.