473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Error on re-opening a form

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
6 1984
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
> 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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
1
by: Aravind | last post by:
we have two files: 1. rc4.c (defines one function "create_pin()") 2. MyImpl.c(calling the function "create_pin()"),This implements JNI method. 1.When I am trying to create .dll file with one...
1
by: yanwan | last post by:
I met this problem in executing a c++ project in visual studio. Does anyone have suggestions to resolve "error lnk 2001"? --------------------Configuration: reconstruction - Win32...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Andrew Luke | last post by:
Hi all you C++ guru's! I'm 'very, very' new to C++ and I'm having a little trouble configuring my VS environment I think - when I try and compile some sample code I'm getting the following...
5
by: azgoddess1 | last post by:
During the installation I get these error messages: ***** SQL1390C The environment variable DB2Instance is not defined or is invalid An error ocured while loading the command "C:\Program...
1
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
4
by: Sanjay Kumar | last post by:
Folks ! I am working with VC++ after a long time and having problem linking latest xerces 2.7 in VC++ 2005 Express Edition. I have done following: 1. downloaded and unpacked the the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.