Izzy said:
You could pass form B too form C using ByRef in the New event of form
C. Then on closing of form C call B.Close()
Incidentally, and this seems to be a very common misconception, there is no
need to pass form B to form C ByRef (by reference). You can (and most likely
should) pass it ByVal (by value).
When you declare an object parameter by value, it is only the pointer to the
object that is passed by value. It is still the same object that it points
to. So in this instance, passing form B to form C by value would not create
a new instance of form B, it would just create a new pointer to the existing
instance.
The only real difference between passing by reference and by value takes
place if the code to which it is passed makes a modification to the pointer,
in which case the original reference will be changed too if passed by
reference, or left unchanged if passed by value.
For example:
\\\
'Class-level form declaration so we can see it at all times
Public a As MyForm
Public Sub Test
'Create a new instance of a form
a = New MyForm
'Call a procedure, passing the object reference by value
MyProcByValue(a)
'Do we still have a valid form reference?
If a Is Nothing Then MsgBox("After call using ByVal, a is Nothing")
'Call a procedure, passing the object reference by reference
MyProcByReference(a)
'Do we still have a valid form reference?
If a Is Nothing Then MsgBox("After call using ByRef, a is Nothing")
End Sub
Private Sub MyProcByValue(ByVal f As MyForm)
'Is this the form we created earlier?
If f Is a Then MsgBox("ByVal: This is our existing form")
'Set f to nothing
f = Nothing
End Sub
Private Sub MyProcByReference(ByRef f As MyForm)
'Is this the form we created earlier?
If f Is a Then MsgBox("ByRef: This is our existing form")
'Set f to nothing
f = Nothing
End Sub
///
Try pasting that into VB and running it. You'll see that in both of the
"MyProc..." procedures, form "f" really is the same form as form "a",
regardless of the ByRef or ByVal parameter. However after returning from the
ByVal procedure, the original object reference "a" will be unchanged, but
after returning from the ByRef procedure it will have been set to nothing.
Hopefully that makes sense anyway.
In my experience there are very few occasions where you need to pass
parameters by reference. It is only when you need to be able to modify the
value in the calling procedure that you should ever do this, and it is well
worth documenting the fact that you have declared a variable in this way and
the reason for doing so. Unnecessary use of ByRef parameters can easily
introduce difficult-to-diagnose errors into your code.
I hope that was of some use.