By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

Determine if a Form is already open.

P: n/a
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.
Oct 1 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
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.

"Greg" wrote:
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.
Oct 1 '07 #2

P: n/a
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

Oct 1 '07 #3

P: n/a
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
"Greg" wrote:
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.
Oct 1 '07 #4

P: n/a
On Sep 30, 9:07 pm, Greg <AccessVBA...@newsgroups.nospamwrote:
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
Oct 1 '07 #5

P: n/a

"Greg" <Ac**********@newsgroups.nospamwrote
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
Oct 1 '07 #6

P: n/a
On Oct 1, 7:49 am, "Larry Serflaten" <serfla...@usinternet.comwrote:
"Greg" <AccessVBA...@newsgroups.nospamwrote
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
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

Oct 1 '07 #7

P: n/a
On Oct 1, 8:10 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Oct 1, 7:49 am, "Larry Serflaten" <serfla...@usinternet.comwrote:
"Greg" <AccessVBA...@newsgroups.nospamwrote
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

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

Oct 1 '07 #8

P: n/a
On Oct 1, 9:58 am, Chris Dunaway <dunaw...@gmail.comwrote:
On Oct 1, 8:10 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Oct 1, 7:49 am, "Larry Serflaten" <serfla...@usinternet.comwrote:
"Greg" <AccessVBA...@newsgroups.nospamwrote
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
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
Sorry, I defined "Open" as instantiated, I didn't realize the OP meant
"instantiated and shown"

Thanks,

Seth Rowe

Oct 1 '07 #9

P: n/a

"Chris Dunaway" <du******@gmail.comwrote
How can I tell via code if a Form is already open.
How about trying to access a public property on the form?
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.
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
Oct 1 '07 #10

P: n/a
On Oct 1, 10:34 am, "Larry Serflaten" <serfla...@usinternet.com>
wrote:
"Chris Dunaway" <dunaw...@gmail.comwrote
How can I tell via code if a Form is already open.
How about trying to access a public property on the form?
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.
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
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

Oct 1 '07 #11

P: n/a

"rowe_newsgroups" <ro********@yahoo.comwrote
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
Oct 1 '07 #12

P: n/a
On Oct 1, 12:30 pm, "Larry Serflaten" <serfla...@usinternet.com>
wrote:
"rowe_newsgroups" <rowe_em...@yahoo.comwrote
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
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

Oct 1 '07 #13

P: n/a
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.

"Cor Ligthert[MVP]" wrote:
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
Oct 1 '07 #14

P: n/a
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/lib...sdisposed.aspx

Cor

Oct 2 '07 #15

P: n/a
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

Oct 2 '07 #16

P: n/a
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

"rowe_newsgroups" wrote:
On Sep 30, 9:07 pm, Greg <AccessVBA...@newsgroups.nospamwrote:
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
Oct 6 '07 #17

P: n/a
"Greg" <Ac**********@newsgroups.nospamschrieb
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

Oct 6 '07 #18

P: n/a
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.

"Armin Zingler" wrote:
"Greg" <Ac**********@newsgroups.nospamschrieb
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

Oct 7 '07 #19

P: n/a
"Greg" <Ac**********@newsgroups.nospamschrieb
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

Oct 7 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.