Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Determine if a Form is already open.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 01 Oct 2007 02:07 GMT
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.
Charlie - 01 Oct 2007 03:40 GMT
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.    

> How can I tell via code if a Form is already open.
>
[quoted text clipped - 12 lines]
>
> Thank you.
Cor Ligthert[MVP] - 01 Oct 2007 05:50 GMT
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
Greg - 01 Oct 2007 21:32 GMT
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.

> Greg,
>
[quoted text clipped - 4 lines]
>
> Cor
Cor Ligthert[MVP] - 02 Oct 2007 05:21 GMT
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
Charlie - 01 Oct 2007 07:29 GMT
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

> How can I tell via code if a Form is already open.
>
[quoted text clipped - 12 lines]
>
> Thank you.
rowe_newsgroups - 01 Oct 2007 11:38 GMT
> How can I tell via code if a Form is already open.
>
[quoted text clipped - 12 lines]
>
> 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
Greg - 06 Oct 2007 04:28 GMT
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

> > How can I tell via code if a Form is already open.
> >
[quoted text clipped - 35 lines]
>
> Seth Rowe
Armin Zingler - 06 Oct 2007 11:59 GMT
> I'll reply as simply as I can.
>
[quoted text clipped - 10 lines]
> 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
Greg - 07 Oct 2007 01:33 GMT
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.

> > I'll reply as simply as I can.
> >
[quoted text clipped - 19 lines]
>
> Armin
Armin Zingler - 07 Oct 2007 03:12 GMT
> 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
Larry Serflaten - 01 Oct 2007 12:49 GMT
> How can I tell via code if a Form is already open.
>
[quoted text clipped - 5 lines]
> 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
rowe_newsgroups - 01 Oct 2007 14:10 GMT
> > How can I tell via code if a Form is already open.
>
[quoted text clipped - 11 lines]
>
> 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
Chris Dunaway - 01 Oct 2007 14:58 GMT
> > > How can I tell via code if a Form is already open.
>
[quoted text clipped - 19 lines]
>
> 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
rowe_newsgroups - 01 Oct 2007 15:18 GMT
> > > > How can I tell via code if a Form is already open.
>
[quoted text clipped - 25 lines]
>
> Chris

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

Thanks,

Seth Rowe
Larry Serflaten - 01 Oct 2007 15:34 GMT
> > > > 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
rowe_newsgroups - 01 Oct 2007 16:33 GMT
On Oct 1, 10:34 am, "Larry Serflaten" <serfla...@usinternet.com>
wrote:

> > > > > How can I tell via code if a Form is already open.
> > > > How about trying to access a public property on the form?
[quoted text clipped - 8 lines]
>
> 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
Larry Serflaten - 01 Oct 2007 17:30 GMT
> 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
rowe_newsgroups - 01 Oct 2007 18:21 GMT
On Oct 1, 12:30 pm, "Larry Serflaten" <serfla...@usinternet.com>
wrote:

> > 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
[quoted text clipped - 11 lines]
>
> 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
Cor Ligthert[MVP] - 02 Oct 2007 05:26 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.