473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Determine if a Form is already open.

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
19 19013
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
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
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
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

"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
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
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
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

"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
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

"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
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
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
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
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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ST | last post by:
This is my other error when I click on Immunoflourescence. I believe this is related to the other error I just posted (Input string was not in a correct format.) Please let me know if you have...
6
by: Stefania Scott | last post by:
Hi, I need to find how can I know that a program I am going to shell it is already open. This program uses an csv file that I need to refresh before shelling the program, however if the csv file...
3
by: P | last post by:
Hi, Access 2002. I use DoCmd.OpenForm attached to command buttons to open one form from another form. All forms are open in dialog mode. When a user tries to open a form already opened from a...
4
by: dvorett | last post by:
I have a form in my database that is password protected, and several forms contain buttons that open the password protected page. Each button asks for the password, but I dont want access to ask...
2
by: John | last post by:
Hi I am opening a dialog using; frmdialog = New frmdialognew frmdialog.ShowDialog() Is there a way to see if this dialog is already open to avoid opening multiple instances of it?
3
by: able | last post by:
Dear friends Somebody an idea on this matter? I have two forms in my app, form1 and form2 In form1 there is two buttons: Private myForm2 AS New Form2 Private Sub Button1_Click......
9
by: KelsMckin | last post by:
Hello, I was wondering if there was a way to check there was already a form open before opening a new one, sorry that doesnt seem clear, here is an example: The following button opens a new form...
2
by: LB | last post by:
How may i do to know if a form is already open ?? In vb6, i use Forms but i don't know how to do this in vbnet. Thank you!!!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.