Error on re-opening a form

B

bole2cant

The error I get is:
Cannot access a disposed Object named "Form2".
Object name "Form2".

Here is the sequence which gives the error.
Start program. Form1 comes up and has a button to choose Form2.
Go to Form2. Close Form2 when done. Now back on Form1.
Click button to go back to Form2. The error/exception comes up.

I originally got this error when using Me.Close(), so I changed it to Me.Hide() and everything was fine. Except now when the form is closed using the white X in the red button instead of the 'Close' button on Form2, I still get the error message. Apparently, clicking the white X is equivalent to Me.Close() and that won't let me re-open the form.

What am I missing here?


****Code in Form1

Public Class Form1

Inherits System.Windows.Forms.Form

Dim Form2 As New Form2

Dim Form3 As New Form3



---------------------------------------

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Form2.Show()

Form2.BringToFront()

End Sub



****Code in Form2

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.Hide()

End Sub

=============================
 
K

Ken Tucker [MVP]

Hi,

I would not name a form2 variable form2. Try this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If form2 is nothing then form2 = new form2

Form2.Show()

Form2.BringToFront()

End Sub

Ken
----------------
 
R

Richard Myers

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If form2 is nothing then form2 = new form2

Form2.Show()

Form2.BringToFront()

End Sub

This wont always work. You really want

If Form2 Is Nothing OrElse Form2.IsDisposed then Form2 = New Form2

hth
Richard
 
B

bole2cant

Thank you, Ken and Richard.

Yes, Richard it did help! I can now go backward and forward to the forms.

As a novice I don't really understand the code but for now that is okay.

This program is my very first vb.net program and, while it is trivial, I have
learned quite a bit. My only dissatisfaction with it is that after I fill in
the three inputs (or change one) I can't just press 'enter' to activate the
Calculate button.

The .exe will soon be available at:
www.xmission.com/~sherwin/download2.html

If you wish to comment on it feel free--I've got thick skin--well, actually,
since I got scalped for melanoma the skin is very thin, but that is another
story. <g>

-Doug

=======================
 
R

Richard Myers

Yes, Richard it did help! I can now go backward and forward to the forms.
As a novice I don't really understand the code but for now that is okay.

O.k its pretty straightforward. The Form2 Is Nothing part of the conditional
is checking to see that the memory set aside for a Form2 object actually has
something in it. The Form2.IsDisposed is checking to see whether the object
found at the memory space has been disposed.

Disposed is a state that an object enters into after it has been Disposed,
that is had it's disposed method called on it. All forms have a disposed
method, which is a special method that allows the form to clean up any
resource handles it may have acquired during its lifetime. You'll note that
we check whether the Form.IsDisposed after we checked whether the
Form.IsNothing. This is because IsDisposed is a property of a form. If the
form Is Nothing and we did the check the other way around i.e

If Form2.IsDisposed OrElse Form2 Is Nothing then...

we'd get a Null Reference exception because there is nothing there to check
whether it has been disposed. Note the use of the OrElse conditional, called
a "short circuit" conditional. If the first part of the conditional
evaluates to
true it doesn't evaluate the second half. Whereas if we had of just used
"Or" the second half of the conditional would always be evaluated thus
throwing a NullRef Exception when Form2 Is Nothing.


When you open a form using .Show and then call .Close, the form instance is
automatically disposed by the runtime, however when you open a form via
..ShowDialog it is not. Therefore you should always call
MyForm.Dispose on forms opened via .ShowDialog.


With respect to your other question about the enter key, there are a myraid
of ways to accomplish this result. You could either handle the KeyPress
event of the input fields or you could set the Calculate button as the
AcceptButton of the form in question. Just check out the property table for
Form2 and look for accept button.
Note there is also a cancel button as well whose event is fired when a user
hits the Escape key.

Hth

Richard
 
R

Richard Myers

Note i lose my way alittle below.
All forms have a Dispose method, not a Disposed method.
And IsDisposed is a property.

Richard
 
G

Guest

You could also handle the Form2.Closeing. Set e.cancel = true, and then me.hide. That way you will not luse the data on the form.

~Programous
 

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