[RESOLVED] Object Disposed Exception - Multiple Forms
Hey Guys,
I am using VB 2008 Express Edition.
I have an app that has 2 forms (will be adding more). Button click on form1 opens form 2. The first time I run my app everything works fine, but when I click the close 'X' button on form 2 and try opening form 2 again by clicking the button on form 1, VB throws a Object Disposed Exception, Cannot access a disposed object
Here is my code on form 1
Code:
Public Class frmMain
Dim myForm As Form3
Private Sub btnWeld1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeld1.Click
If IsDisposed Then Return
If myForm Is Nothing Then
myForm = New Form3
End If
myForm.Show()
End Sub
End Class
Can you tell me how to fix this problem.
Thanks,
Novice_User
Last edited by Vb_Novice_User; Jan 21st, 2010 at 12:54 PM.
Is there anything special with Form3 Finalizer?
Your code works fine with an empty form3 (by the way, is it a typo? You use the name Form3, not Form2 in your code).
I tried the same code on a fresh app, it still throws in the same error.
Initially I had only form2. I still have it as a backup, but it is not linked to form1. My form2 and form3 have the same code. I am just keeping form2 in case I goof up on form3 (like accidentally delete something).
Weird.
do you have anything in the Form3 Sub Form_Load or Sub New ?
This code works perfectly.
It's probably him loading it twice. It works at first, then fails the second time. The reason is because he's declaring the variable in the Class scope. Then he opens the form. Once the form is closed, the variable is disposed. He either has to use the default instance of the new form or call the variable declaration within the sub scope.
Great. Please remember to mark the thread resolved by selecting Resolved from Thread Tools near the top.
Also, if you're declaring the variable within the sub scope, each time you close the form you're disposing of that variable. So each time you open the form, you're using a new instance of that form. Which means anything you change on that form won't stay. That is, of course, unless you're using My.Settings to save the information.
If you're declaring the variable within the sub, then you probably won't need the disposed check.
Great. Please remember to mark the thread resolved by selecting Resolved from Thread Tools near the top.
I still don't understan why this code works for me? I created a new project, then create two forms and copypasted that code. It worked Each time I pressed a button Form3 appeared again.
Dim myForm As Form3 (it is now Nothing)
First button click...
myForm Is Nothing so myForm = New Form3 (since it was "nothing", it will now be set to a new instance of Form3)
myForm.Show (Show the form)
Close the form.
myForm is STILL an instance of Form3, but it has been Disposed. It will not be set to Nothing until AFTER the garbage collect cleans it out.
Click button again...
myForm doesn't change since it is NOT NOTHING.
myForm.Show (ERROR... because you can't call Show on a Disposed form object.)