Q: closing forms

G

G .Net

Hi

I'm wondering if anybody can help me with the following problem.

Suppose I have 3 forms: A, B and C.

Pressing a button on form A, shows (using ShowDialog) form B.

Pressing a button on form B, shows (using ShowDialog) form C.

I want to have a button on form C which, when pressed, closes form C and
form B, revealing form A.

Can anybody help? Some example code would be great.

Thanks in advance

G
 
I

Izzy

There are several ways to do this.

You could publicly declare your forms in a module too make them
accessible anywhere.
On Sub Main open the first form.

Then write a sub called "OpenForm(byRef f as form)"
Then write another called "CloseForm(ByRef f as form)"

You get the idea.

OR

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()


Izzy
 
G

G .Net

Thanks Izzy. That's a great idea!

Izzy said:
There are several ways to do this.

You could publicly declare your forms in a module too make them
accessible anywhere.
On Sub Main open the first form.

Then write a sub called "OpenForm(byRef f as form)"
Then write another called "CloseForm(ByRef f as form)"

You get the idea.

OR

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()


Izzy
 
O

Oenone

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.
 
I

Izzy

Interesting....

Let me ask you this, if we pass form B into form C using ByVal:

A) Does this NOT use any additional memory?
B) Does calling B.Close() and B.Dispose() within form C, actually close
and dispose of the original object? Just like calling it from form B
itself.

Thanks, for jumping in. Because I was under the impression using ByVal
creates a seperate object like the first. This is good to know.

Thanks,
Izzy
 
C

Cor Ligthert [MVP]

G.

Ever tried it like this.
\\\Form B
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim frmB As New Form2
Dim A As Integer = frmB.ShowDialog()
If A = Windows.Forms.DialogResult.OK Then
'do what you want to do
ElseIf A = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
///
\\form C
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
///
I hope this helps,

Cor
 
C

Cor Ligthert [MVP]

Izzy,
A) Does this NOT use any additional memory?

Not with a reference type, a value is always passed by its value
B) Does calling B.Close() and B.Dispose() within form C, actually close
and dispose of the original object? Just like calling it from form B
itself.
Why do you not try that yourself, I never do it that way,

Cor
 
O

Oenone

Izzy said:
Interesting....

Let me ask you this, if we pass form B into form C using ByVal:

A) Does this NOT use any additional memory?

It uses the additional memory required to create a new pointer to the object
(i.e., 4 bytes on 32-bit Windows). Not a huge overhead. :)
B) Does calling B.Close() and B.Dispose() within form C, actually
close and dispose of the original object? Just like calling it from
form B itself.

Yes -- as it is still the original object. It's only the pointer to the
object that's different.

Let's see if an analogy helps -- hopefully this isn't too twisted. :)

Imagine that there is a street full of houses, and one of the houses is 123
Coder Street. You've written the address of 123 Coder Street on a piece of
paper.

If you pass me the address of the house by reference, you are actually
handing me your piece of paper. I can then go to the house and paint the
front door blue. If I change what is written on the paper to a different
address, when I give it back to you then your piece of paper will contain my
modification.

Alternatively you can pass me the paper by value, by writing the address on
a new piece of paper and giving me that. It still reads "123 Coder Street"
and I can still go to the house and paint the front door, it's exactly the
same house as before. The difference is that when I've finished with my own
piece of paper, I'll throw it away instead of giving it back to you, so if I
decide to change the address on the paper, your own piece of paper won't be
affected.
Thanks, for jumping in. Because I was under the impression using ByVal
creates a seperate object like the first. This is good to know.

No problem -- this does seem to be something that a very large number of
people misunderstand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top