all groups > vb.net upgrade > july 2004 >
You're in the

vb.net upgrade

group:

loading forms only once.


loading forms only once. TerryW <prog
7/30/2004 2:59:29 PM
vb.net upgrade: I am trying to write my first app using dotnet after years of vb6.

I am trying to load a form from a "main" form but after I have loaded
one instance of the form's class I don't want to load any more. Do I
have to keep track of which classes I have instantiated or is there a
better way of doing this?

TIA
Re: loading forms only once. Éric Moreau [VB MVP]
7/30/2004 10:38:49 PM
A MDI application? In this case, you can iterate through the MdiChildren
collection to see if it is already in memory:

Private Function IsChildInMemory(ByVal pChildName As String) As Boolean

'A small method that checks to see if a particular form is currently
displayed as a child form

Dim frmChild As Form

For Each frmChild In Me.MdiChildren

If TypeName(frmChild).ToUpper = pChildName.ToUpper Then

frmChild.Activate()

Return True

End If

Next

Return False

End Function



Then, when you want to load a child, you can use it like this:

If Not IsChildInMemory("frmChild2") Then

Dim frm As New frmChild2

With frm

..MdiParent = Me

..Show()

'.Dock = DockStyle.Fill

End With

End If


--

HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Concept S2i inc.(www.s2i.com)


[quoted text, click to view]

Re: loading forms only once. hirf-spam-me-here NO[at]SPAM gmx.at
8/2/2004 7:49:01 AM
* TerryW <prog<NOSPAM>@vbssys.com> scripsit:
[quoted text, click to view]

You could implement the Singleton design pattern to provide only a
single instance:

Exploring the Singleton Design Pattern
<URL:http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
AddThis Social Bookmark Button