Dear Brian,
Thanks for your excellent suggestion to hide the form by handling the
Closing event as you suggested. It worked well.
If I may, I’d like to follow-up with another question:
You also mentioned the other possible solution of creating a new instance of
Form2 each time I show it. That actually may be a better solution for my
particular application, but I don’t understand how that would work:
As I said, I currently have this:
- Form 1 at the top has:
Dim frmTwo As New Form2
(and upon a button click, shows Form2:)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
frmTwo.Show()
How do I implement your idea? Create a whole bunch of Form2 instances like
this:
Dim frmTwo2 As New Form2
Dim frmTwo3 As New Form2
Dim frmTwo4 As New Form2 ... etc. and then somehow keep track of which have
been already Shown & Closed/Disposed and then use the next one up?
That seems hard to imagine for me. My users might be on my application all
day long, and click for Form2 100 times, or who knows how many times before
closing the application.
There’s a lot of logic I use to construct how Form2 looks, based on user
selections done in Form1 (combo-boxes, etc). Right now this is mostly done
in the Load Event for Form2. I noticed that on implementing your solution
you gave me (hide & re-show the one instance), upon re-showing the form2 no
Event seems to get invoked at all (at least not Load or GetFocus, I noticed).
So I would need to invoke all that logic in Form1 right before re-showing
Form2. I can live with that I guess, but perhaps your other idea is better
(creating a new instance of Form2 each time I show it.)
Thanks.
--
RG
"Brian Gideon" wrote:
RG,
Closing a form disposes it (unless it was shown as a dialog). You
either need to create a new instance of Form2 each time you show it or
hide it instead of closing it. You can hide it by handling the Closing
event.
Private Sub Form2_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
e.Cancel = True
Me.Hide()
End Sub
Brian
RG wrote: Hello - I'm relatively new to VB and I'm not getting how to do Dispose
correctly from my readings.
I have an application with 3 Forms (with a lot of logic going on within
each): Form1, Form2, and a 3rd Form which is a dialog.
- Form 1 at the top has:
Dim frmTwo As New Form2
(and upon a button click, shows Form2:)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
frmTwo.Show()
- Form 2, upon a button click, shows a Dialog with ShowDialog. I can show
and close and re-show that Dialog all I want. After the ShowDialog statement
I have a Dispose() statement for the Dialog form.
Here's the problem: if I close Form2, and then later I try to Show Form2
again (by clicking the button on Form1) I get the error:
"unhandled exception System.ObjectDisposedException in
system.windows.forms.dll.
Can't access disposed object named Form2 ".
I've tried putting Dispose & Finalize in various places, but I need some
advice here.
Thanks so much.
--
RG