[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

BeeJ

4/1/2012 1:36:00 AM

I load a Form from my Main Form using

Main Form Globals
Dim fAbout = frmAbout

Main Form Sub
Sub AboutOpen()

If fAbout Is Nothing Then
Set fAbout = New frmAbout
End If
fAbout.Show , Me

End Sub

Then I close the form in frmAbout using the Form_Unload event.

But the form is not out of memory.

If I put

Set fAbout = Nothing

in the frmAbout Form_Unload event then the form does go out of memory.
But that means the instance of frmAbout has to know who it is.
Is there a "generic" way to do this?

There are timers in frmAbout but they are not running (I put breaks in
the timer routines to make sure).
No files are open in the frmAbout code.
I cannot think of anything else that could keep the form in memory.

And I do not want frmAbout to be modal.

How do I know the form is still in memory?
If I call AboutOpen() again one of the module level variables in
frmAbout is still set.

--
Noah's Ark was built by amateurs,
The Titanic was built by professionals.
Row, row, row your boat gently down the stream ...
Life is but a dream!


26 Answers

Jim Mack

4/1/2012 1:50:00 AM

0

> I load a Form from my Main Form using
>
> Main Form Globals
> Dim fAbout = frmAbout
>
> Main Form Sub
> Sub AboutOpen()
>
> If fAbout Is Nothing Then
> Set fAbout = New frmAbout
> End If
> fAbout.Show , Me
>
> End Sub
>
> Then I close the form in frmAbout using the Form_Unload event.
>
> But the form is not out of memory.
>
> If I put
>
> Set fAbout = Nothing
>
> in the frmAbout Form_Unload event then the form does go out of memory.

Maybe I'm missing something, but in this example fAbout and frmAbout
are two different objects. If there's a "frmAbout" about, closing it
would have no effect on fAbout.

FWIW, "Dim fAbout = frmAbout" is bad syntax. I assume you mean "as"
instead of "="

--
Jim


BeeJ

4/1/2012 2:05:00 AM

0

Jim Mack presented the following explanation :
>> I load a Form from my Main Form using
>>
>> Main Form Globals
>> Dim fAbout = frmAbout
>>
>> Main Form Sub
>> Sub AboutOpen()
>>
>> If fAbout Is Nothing Then
>> Set fAbout = New frmAbout
>> End If
>> fAbout.Show , Me
>>
>> End Sub
>>
>> Then I close the form in frmAbout using the Form_Unload event.
>>
>> But the form is not out of memory.
>>
>> If I put
>>
>> Set fAbout = Nothing
>>
>> in the frmAbout Form_Unload event then the form does go out of memory.
>
> Maybe I'm missing something, but in this example fAbout and frmAbout are two
> different objects. If there's a "frmAbout" about, closing it would have no
> effect on fAbout.
>
> FWIW, "Dim fAbout = frmAbout" is bad syntax. I assume you mean "as" instead
> of "="

Oops, yes code is AS not =

frmAbout is the form.
fAbout is the instance of frmAbout
Nothing is done to frmAbout.

--
Noah's Ark was built by amateurs,
The Titanic was built by professionals.
Row, row, row your boat gently down the stream ...
Life is but a dream!


ralph

4/1/2012 2:54:00 AM

0

On Sat, 31 Mar 2012 18:35:44 -0700, BeeJ <nospam@spamnot.com> wrote:

>I load a Form from my Main Form using
>
>Main Form Globals
> Dim fAbout = frmAbout
>
>Main Form Sub
> Sub AboutOpen()
>
> If fAbout Is Nothing Then
> Set fAbout = New frmAbout
> End If
> fAbout.Show , Me
>
> End Sub
>
>Then I close the form in frmAbout using the Form_Unload event.
>
>But the form is not out of memory.
>
>If I put
>
> Set fAbout = Nothing
>
>in the frmAbout Form_Unload event then the form does go out of memory.
>But that means the instance of frmAbout has to know who it is.
>Is there a "generic" way to do this?
>

That is the "generic" way.

You have declared an object reference to type frmAbout.
Then you assigned a concrete "instantiated" frmAbout object to that
reference.
To remove the reference you need to set the object reference to
Nothing.
The object reference is known only to the client. There is nothing
(pun intended) an object can do to effect that reference, ie. They can
not commit suicide.

This might help:
"Life Cycle of Visual Basic Forms"
http://msdn.microsoft.com/en-us/librar...(v=vs.60).aspx

To know if something is in memory you can simply query it using the
form object reference and look for error, or see if it is Nothing, but
in normal usage it is easy enough to just track the object reference.

Since an About box is normally just displayed then immediately
destroyed then I probably do something like this ...

Sub AboutOpen()
Set fAbout = New frmAbout
fAbout.Show , Me
Set fAbout = Nothing
End Sub

But this is basic VB 101 stuff, so knowing you and your love of "the
cute" - what is really the problem? Why is this an issue? Give us the
REAL complete story. <bg>

-ralph

mm

4/1/2012 3:06:00 AM

0

"BeeJ" <nospam@spamnot.com> escribió en el mensaje
news:jl8bdp$eb4$1@speranza.aioe.org...
>I load a Form from my Main Form using
>
> Main Form Globals
> Dim fAbout = frmAbout
>
> Main Form Sub
> Sub AboutOpen()
>
> If fAbout Is Nothing Then
> Set fAbout = New frmAbout
> End If
> fAbout.Show , Me

What I do I those cases is:

If fAbout Is Nothing Then
Set fAbout = New frmAbout
fAbout.Show, Me
Do Until Not FormIsLoaded(fAbout) ' it emulates modal in some way
DoEvents
Sleep 10
Loop
Set fAbout = Nothing
Else
fAbout.ZOrder
fAbout.SetFocus
End If

'*** in a Module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Function FormIsLoaded(nForm As Form) As Boolean
Dim frm As Form

For Each frm In Forms
If frm Is nForm Then
FormIsLoaded = True
Exit For
End If
Next frm
End Function


mm

4/1/2012 5:02:00 AM

0

Or don't set the form as New and just use the default instance:

frmAbout.Show, Me

and in the Form_Unload event of the form:

Set frmAbout = Nothing


Ulrich Korndoerfer

4/1/2012 12:25:00 PM

0

Hi,

BeeJ schrieb:
> I load a Form from my Main Form using
>
> Main Form Globals
> Dim fAbout = frmAbout
>
> Main Form Sub
> Sub AboutOpen()
>
> If fAbout Is Nothing Then
> Set fAbout = New frmAbout
> End If
> fAbout.Show , Me
>
> End Sub
>
> Then I close the form in frmAbout using the Form_Unload event.
>
> But the form is not out of memory.
>
> If I put
>
> Set fAbout = Nothing
>
> in the frmAbout Form_Unload event then the form does go out of memory.
> But that means the instance of frmAbout has to know who it is.
> Is there a "generic" way to do this?
> ...

Private Sub AboutOpen()
Dim fAbout As frmAbout

Set fAbout = GetLoadedInstance

If fAbout Is Nothing Then
Set fAbout = New frmAbout
fAbout.Show , Me
End If

End Sub

Private Function GetLoadedInstance() As frmAbout
Dim CurrentForm As Form

For Each CurrentForm In Forms
If TypeOf CurrentForm Is frmAbout Then
Set GetLoadedInstance = CurrentForm
Exit For
End If
Next CurrentForm
End Function

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.prosource.de/...
MS Newsgruppen Alternativen -> http://www.prosource.de/ms-ng-...

Larry Serflaten

4/1/2012 12:57:00 PM

0

BeeJ wrote:

> in the frmAbout Form_Unload event then the form does go out of memory.
> But that means the instance of frmAbout has to know who it is.
> Is there a "generic" way to do this?


If you are showing the form for information and do not need to reference it
later, one simple method goes like this:

Sub ShowAbout()
With New frmAbout
.Show
End With
End Sub

VB creates the form (With New frmAbout) and destroys the object
reference (End With).

That will cause it to be removed from memory when properly closed.

LFS

GS

4/1/2012 4:41:00 PM

0

I observe the following *rules* when creating objects so I don't have
these kinds of problems.

Rule1:
Explicitly created objects should be explicitly destroyed.

Rule2:
Implicitly created objects should be implicitly destroyed.

Rule3:
Know when use of either of the above is appropriate!

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


BeeJ

4/1/2012 9:06:00 PM

0

See Post Below

--
Noah's Ark was built by amateurs,
The Titanic was built by professionals.
Row, row, row your boat gently down the stream ...
Life is but a dream!


BeeJ

4/1/2012 9:11:00 PM

0

Simplified ( would normally use Property Get Let
I can do this for an array of forms.

How to do if want to use WithEvents for frmAbout?
How to do if not using an array of frmAbout?

' ====================
' FORM MAIN frmMain
' ====================
Option Explicit

Private Sub cmdCheck_Click()

' COMMAND BUTTON

Dim lF As Long
Dim lInstance As Long

For lF = 0 To UBound(fAbout)

If fAbout(lF) Is Nothing Then
Message lF & " Index is Nothing"
Else
lInstance = fAbout(lF).g_lInstance
Message lF & " Index Exists As Instance " & lInstance & "
Caption " & fAbout(lF).Caption
End If

Next lF

End Sub 'cmdCheck_Click

Private Sub Message(sMsg As String)

' LISTBOX
lstMsg.AddItem sMsg
lstMsg.ListIndex = lstMsg.ListCount - 1

End Sub 'Message

Private Sub Form_Load()

ReDim fAbout(0 To 2) As frmAbout

If fAbout(0) Is Nothing Then
Set fAbout(0) = New frmAbout
End If
fAbout(0).Caption = "1st"
fAbout(0).g_lInstance = 0
fAbout(0).Show , Me

' |-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|

If fAbout(1) Is Nothing Then
Set fAbout(1) = New frmAbout
End If
fAbout(1).Caption = "2nd"
fAbout(1).g_lInstance = 1
fAbout(1).Show , Me

' |-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|

If fAbout(2) Is Nothing Then
Set fAbout(2) = New frmAbout
End If
fAbout(2).Caption = "3rd"
fAbout(2).g_lInstance = 2
fAbout(2).Show , Me

End Sub 'Form_Load

' ==========================
' FRMABOUT frmAbout
' ==========================

Option Explicit

Public g_lInstance As Long ' would use Property Let Get

Private Sub cmdClose_Click()

' COMMAND BUTTON
Unload Me

End Sub 'cmdClose_Click

Private Sub Form_Unload(Cancel As Integer)

Set fAbout(g_lInstance) = Nothing

End Sub 'Form_Unload

' ========================
' mdlMain
' ========================

Option Explicit

Public fAbout() As frmAbout

--
Noah's Ark was built by amateurs,
The Titanic was built by professionals.
Row, row, row your boat gently down the stream ...
Life is but a dream!