By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

Error on re-opening a form

P: n/a
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

=============================
--
-Doug

Thanks Herfried for help with formatting decimals.
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
----------------

"bole2cant" <sh*****@xxxmission.com> wrote in message
news:40**********************@news.xmission.com:
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

=============================


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.9 - Release Date: 7/2/2004
Nov 20 '05 #2

P: n/a
> 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
Nov 20 '05 #3

P: n/a
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

=======================

"Richard Myers" <ri*********************@basd.co.nz> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #4

P: n/a
> 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


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

=======================

"Richard Myers" <ri*********************@basd.co.nz> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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



Nov 20 '05 #5

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

Richard
Nov 20 '05 #6

P: n/a
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

"bole2cant" wrote:
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

=============================
--
-Doug

Thanks Herfried for help with formatting decimals

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.