Determine if a Form is already open.

G

Guest

How can I tell via code if a Form is already open.

Each time my forms load I have some initialization code that runs, but if
the form is already open and hidden I don't want that initialization code to
run. Thus, I just want to unhide the form.

My thought is eveyrtime I go to load a form I could scan through all the
open forms in my project and determine whether the one I'm getting ready to
load is open or not.

So, this is a two part question.

1. How can I tell if a form is open?
2. How can I loop through all open forms?

Thank you.
 
G

Guest

Create a friend module called modForms.
Add a friend hashtable called Forms.

Friend Module modForms
Friend Forms As New Hashtable()
End Module

In the Form_Load event for each form...

Me.Name = "FormName"
modForms.Forms.Add(Me.Name, Me)

This will add a reference in a central location accessible to all files in
the current project.

When you want to open a form...

Dim F As Form
If Not IsNothing(modForms.Forms("FormName")) Then
F = modForms.Forms("FormName")
If F.Visible = False Then
F.Show()
else
F.BringToFront()
End If
Else
F = New FormNameObject()
F.Show()
End If

To make life easier, use the class name of the form for the Name property

NOTE: This code only only creates a new instance of the form if it does not
yet exist. The object variables serve only as references to the form.

If the initialation code is in the New sub, or Form_Load, it will only run
once using this code.
 
C

Cor Ligthert[MVP]

Greg,

Why you want all forms open, is this not a kind of spooiling performance and
memory?

In my idea for sure not OOP, more a kind of Modulair programming using Net.

Cor
 
G

Guest

If you decide to use my suggested code, you might want to add code to remove
the hashtable reference to the form in the form's closing event.

Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
modForms.Forms.Remove("Form1")
End Sub
 
R

rowe_newsgroups

How can I tell via code if a Form is already open.

Each time my forms load I have some initialization code that runs, but if
the form is already open and hidden I don't want that initialization code to
run. Thus, I just want to unhide the form.

My thought is eveyrtime I go to load a form I could scan through all the
open forms in my project and determine whether the one I'm getting ready to
load is open or not.

So, this is a two part question.

1. How can I tell if a form is open?
2. How can I loop through all open forms?

Thank you.
From what you say I gather the Form you want to determine if it's
opened is supposed to be a single instance form? In other words, you
never want to have more than one "copy" of the form running at a time
right? If so I would recommend not keeping an internal list of opened
forms, as this approach is tedious and error prone and difficult to
maintain (in my opinion). Instead a much simpler approach would be to
just implement the Singleton Pattern on the form. My favorite
implementation has come from Jon Skeet who is a huge resource over in
the C# newsgroup. While his sample is in C# it should be easy enough
to convert (http://www.yoda.arachsys.com/csharp/singleton.html) but if
you need help just ask.

As far as the looping through all open forms question goes, Charlie
has already shown you how to use a hashtable to track the forms.
Though IIRC, doesn't VB 2005 have a My.Forms property that does this
automatically? I never use the My namespace since it isn't directly
available in the other .Net languages so I could be completely wrong.
Also, why exactly do you need to know all open forms?

Thanks,

Seth Rowe
 
L

Larry Serflaten

Greg said:
How can I tell via code if a Form is already open.

Each time my forms load I have some initialization code that runs, but if
the form is already open and hidden I don't want that initialization code to
run. Thus, I just want to unhide the form.

My thought is eveyrtime I go to load a form I could scan through all the
open forms in my project and determine whether the one I'm getting ready to
load is open or not.

How about trying to access a public property on the form?

If you can, it must be loaded, if not it will throw an error....

LFS
 
R

rowe_newsgroups

How about trying to access a public property on the form?

If you can, it must be loaded, if not it will throw an error....

LFS

Because you need an instance of the form to call the public property.
And if you had an instance of the form you could simply do an If
instance IsNot Nothing Then... test.

Thanks,

Seth Rowe
 
C

Chris Dunaway

Because you need an instance of the form to call the public property.
And if you had an instance of the form you could simply do an If
instance IsNot Nothing Then... test.

Thanks,

Seth Rowe

Just because an instance is not Nothing does not mean that the form is
Open. Checking the Visible property should be an accurate way to tell
if the form is open.

Chris
 
R

rowe_newsgroups

Just because an instance is not Nothing does not mean that the form is
Open. Checking the Visible property should be an accurate way to tell
if the form is open.

Chris

Sorry, I defined "Open" as instantiated, I didn't realize the OP meant
"instantiated and shown"

Thanks,

Seth Rowe
 
L

Larry Serflaten

Just because an instance is not Nothing does not mean that the form is
Open. Checking the Visible property should be an accurate way to tell
if the form is open.

Nice save Chris. :)

LFS
 
R

rowe_newsgroups

Nice save Chris. :)

LFS

But this still requires us to have an instance of the form to check,
which I believe is the problem the OP needs resolved. You can't simply
just check the Visible property of form if you don't have a way of
getting a hold of a Form's created instances.....

Thanks,

Seth Rowe
 
L

Larry Serflaten

rowe_newsgroups said:
But this still requires us to have an instance of the form to check,
which I believe is the problem the OP needs resolved. You can't simply
just check the Visible property of form if you don't have a way of
getting a hold of a Form's created instances.....

It seems wasteful to me, to create a form and then let the variable
fall out of scope. If the OP only wanted one instance of the form
showing in the application, I would think the better design would
make that form reference available to all the other code so that the
other code has access to the form when needed....

In that case, the variable typed as the desired form would be
available to do the tests.

LFS
 
R

rowe_newsgroups

It seems wasteful to me, to create a form and then let the variable
fall out of scope. If the OP only wanted one instance of the form
showing in the application, I would think the better design would
make that form reference available to all the other code so that the
other code has access to the form when needed....

In that case, the variable typed as the desired form would be
available to do the tests.

LFS

Which is why I recommended using the Singleton pattern. This would
handle the entire operation, as it would allow only one instance of
the form and would give all other "users" of the form easy access to
the object instance.

Thanks,

Seth Rowe
 
G

Guest

I'm not keeping all of my forms open. I want to have a common procedure that
handles open and closing forms. For example. Say Form1 is loaded as an
MIDChild form. Now, I select an option to open Form2. I want to close Form1,
buit I don't want to have to specifically reference Form1 to close it, I want
to scan the system for alll open forms and close them, except for my main
MDIParent form.
 
C

Cor Ligthert[MVP]

Greg,

I was not sure it was about MDI forms, in my idea did you not write that.

You can reach MDI forms using the MDIChildren collection. While you can
reach the parent using the MDIParent. By using those references you can get
almost everything about MDI forms.

Do not forget the very well hidden member "IsDisposed", with that you can
see if a form is still available.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.isdisposed.aspx

Cor
 
C

Cor Ligthert[MVP]

Larry,

What you want to create are in my idea modulair forms, in my idea just the
oposite of OOP.

What is wrong with that, could be your next question. My answer is than, you
are not controling the solution anymore and make it with that less
maintanable.

Cor
 
G

Guest

I'll reply as simply as I can.

I have a form open in a MDIParent Form. For the sake of this question, lets
assume I have no idea what the name of the form is. Now, I have decided to
open another form, so I want to close the currently open form, of which I am
not tracking the name of, so I want to be able to scan for all open forms (or
in this case) scan to see what form is currently open and close it before I
open my other form. I can certainly keep track of the name of the form and
handle it that way, but I want a procedure that will just close ANY open
form(s) within the MDIParent or even in a non MIDParent environment.

Nothing to complexe, just a simple routine. Maybe this is the wrong thing to
do in VB.Net?

Thanks
 
A

Armin Zingler

Greg said:
I'll reply as simply as I can.

I have a form open in a MDIParent Form. For the sake of this
question, lets assume I have no idea what the name of the form is.
Now, I have decided to open another form, so I want to close the
currently open form, of which I am not tracking the name of, so I
want to be able to scan for all open forms (or in this case) scan to
see what form is currently open and close it before I open my other
form. I can certainly keep track of the name of the form and handle
it that way, but I want a procedure that will just close ANY open
form(s) within the MDIParent or even in a non MIDParent environment.

Nothing to complexe, just a simple routine. Maybe this is the wrong
thing to do in VB.Net?

It is simple, though you are the one who opens the forms, aren't you? If you
don't store information (in a variable referncing the Form), it gets lost.
It's like saying: I'm creating an array, don't store a reference but I still
want to access it. There is nothing special about Forms, so I'd do the same.

With MDI chidren, have a look at the MDI parent's MdiChildren property.


Armin
 
G

Guest

I already hold a reference to the form I have open and currenlty use that to
close it. I was just wanting to create a procedure that would close "any"
form that happened to be open, without having to specificaly reference it.

Thanks.
 
A

Armin Zingler

Greg said:
I already hold a reference to the form I have open and currenlty use
that to close it. I was just wanting to create a procedure that
would close "any" form that happened to be open, without having to
specificaly reference it.

Thanks.


Does the Mdichildren property help?


Armin
 

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