How do I set form equal to nothing?

M

Mike Edgewood

Check this out: [VS 2005 .NET 2.0]

Start a new project, drop a button on form1. Add a form (form2). In
the button1 click event on form1, add the following...
........
If My.Forms.Form2 Is Nothing Then
My.Forms.Form2.Show()
End If
........

Taken directly from sample code. This will only run once and Form2 can
never be shown again.

I like the idea here, but how do I get it to behave correctly?
 
M

Mike Edgewood

If you wouldn't mind, I could really use some help on this one.
Although I appreciate the guess, putting ME = nothing or form2 =
nothing in the closing event of form2 will result in compile error.

Apparently closing form2 does not, by default, make it nothing and must
specifically set to nothing.

In VS2003 I attempted creating single instance forms by a public shared
function on the form in question and never referencing the form
directly. ie. My Employee Form...

Private Shared mEmpForm as frmEmployees = Nothing
Public Shared Function GetForm(ByRef MainForm as frmMain) as
frmEmployee
If mEmpForm is Nothing then
mEmpForm = New frmEmployees
mEmpForm.MdiParent = MainForm
End If
mEmpForm.Activate
Return mEmpForm
End Function

Also, you need to put the following in the forms closing event of the
form...
mEmpForm = Nothing.

I like the VS2005 way much better. What do I need to do to make the
form nothing when I close it?
 
H

Herfried K. Wagner [MVP]

Mike Edgewood said:
Check this out: [VS 2005 .NET 2.0]

Start a new project, drop a button on form1. Add a form (form2). In
the button1 click event on form1, add the following...
.......
If My.Forms.Form2 Is Nothing Then
My.Forms.Form2.Show()
End If
.......

Taken directly from sample code. This will only run once and Form2 can
never be shown again.

Well, that's by design. After showing the form for the first time the
variable will point to the form's default instance.

You may want to use this code instead:

\\\
Dim f As New Form2()
f.Show()
///
 
M

Mike Edgewood

Keep in mind that the code I started with is from a sample from
Microsoft. Specfically, the "Using My" sample included in the 101 code
samples VS 2005 set.

Based upon the logic of this sample, it is apparent to me that someone
wrote this to show me/us how a sigle instance form can be achieved.
However, it does not work as expected. Using this particular sample as
a starting ground, what needs to be modifed *in this sample* to allow
it to perform/behave as expected?

Anxious to see someone's solution to this dilema.
 
G

Guest

If My.Forms.Form2 Is Nothing Then
My.Forms.Form2.Show()
ElseIf My.Forms.Form2.IsDisposed = True
My.Forms.Form2.Show()
End If

may work...
 
M

Mike Edgewood

That's not going to work.

Start a new project, drop a button on form1. Add a form (form2). In
the button1 click event on form1, add the following...
........
If My.Forms.Form2 Is Nothing Then
My.Forms.Form2.Show()
End If
........

This will only run once and Form2 can never be shown again. I like the
idea here, but how do I get it to behave correctly?
 
M

Michael Höhne

Mike,

If you're talking about the "correct" behaviour, what exactly are you
expecting?

If you want to show the same Form2 whenever you press the button in form1, I
expect that simply calling

My.Forms.Form2.Show()

should work (without the test for nothing).

If you always want to display a new Form2,

Dim newForm as new Form2()
newForm.Show()

should work.

If it still does not work, maybe a

private static Form2 form2Instance;

would help. Though this is C# syntax I think you get the point. I personally
think that if the code

If My.Forms.Form2 Is Nothing Then
My.Forms.Form2.Show()
End If

works at all, then shame on VB. It really reads like if (object == null)
then object.DoSomething().

Michael
 
H

Herfried K. Wagner [MVP]

Brian P. Hammer said:
After you close the form, have you tried to dispose it?

Forms shown by calling their 'Show' method are disposed automatically when
they get closed.
 
M

Mike Edgewood

Yes, but after closing Form2, my.Forms.Form2.IsDisposed equates to
False. So am I wating for garbage collection to take place before
this results in True?

I would assume that the author of the sample in question wanted to show
how to set up a form that could only have one instance of itself and
show off new features of VS 2005. However the short comming is that it
only opens the child/sub forms once for the life of the program and
connot be opened again. How would one modify this code to behave as
the orignal author had intended it to?

I'm afraid I was't a beta tester and this is my first experience with
2005 / 2.0 code. If there is a new feature that allows you to
open/control/maintain single instance child forms that is easier than
the way I am doing it now (mentioned previously) I would love to learn
about it.
 
B

Brian P. Hammer

Very true but I was under the impression that this might happen in 1 second
or at some other time in the future?!? Is this not the case?

Brian
 

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