Miguel Ribeiro <nikron@noemail.noemail> wrote:
<snip>
[color=blue]
> Dim frmControl As Control
> For Each frmControl In pnlPanel.Controls
> If TypeOf frmControl Is Form Then
> If frmControl.Name <> objForm.Name Then ' If
> this is not the form I want to load in
> the panel, then destroy it
> CType(frmControl, Form).Close()
> CType(frmControl, Form).Dispose()
> frmControl = Nothing ' It doesn't seem to work
> Application.DoEvents()
>
> End If
> End If
> Next
>
> It is at this point that I am stuck, when I set all the other forms to
> nothing (via the collection) the variable that references the form is
> not set to nothing. I thought if I deleted the underlying object (by
> setting it to nothing? and Dispose etc) that it would clear all the
> references to that object in memory.[/color]
You can never set an *object* to nothing - you can only set the value
of a *variable* to nothing. That in itself doesn't change any other
variables.
In this case, it sounds like you just want to add:
pnlPanel.Controls.Remove(frmControl)
However, you almost certainly (I haven't tried) can't do that within
the For Each loop. I suggest you build up a list of controls you want
to remove, then remove them all at the end.
Now, if you've still got other variables with references to the form,
you'll have to clear those up separately - there's no automatic way of
finding all the variables with a reference to an object and clearing
them.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too