|
-
Feb 8th, 2012, 02:54 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Cannot access disposed form?
Hello
Code:
If TextBox1.Text = "Welcome!" Then
MsgBox("Welcome!")
Else
MsgBox("Error!")
Me.Close()
End If
I receive "Cannot access a disposed object (Form1)" when the error arrives.
-
Feb 8th, 2012, 03:01 PM
#2
Re: Cannot access disposed form?
With the information you have provided we can not help. What line does the error happen on? When do you call this method? You need to post more code from your project.
-
Feb 8th, 2012, 03:09 PM
#3
Thread Starter
Hyperactive Member
Re: Cannot access disposed form?
I put the code in my form1_load event. When MsgBox ("Error") pops up, I press OK, and then I receive the error:
Cannot access a disposed object.
I'm guessing it's because the form hasn't actually loaded, and I'm calling me.close on technicaly a msgbox.
-
Feb 8th, 2012, 03:41 PM
#4
Re: Cannot access disposed form?
The code above should not throw an exception. Although the logic of checking a textbox value when the form is loading is off. Use The Shown event.
What line is throwing the exception. As i said we really need to see more of your project.
-
Feb 8th, 2012, 04:23 PM
#5
Re: Cannot access disposed form?
It will appear on the Me.Close, but it actually happens elsewhere.
The core problem is that you shouldn't be doing something like that in the load event. Since the example is clearly contrived, we can't really tell you where it should be moved to, but Load is wrong. The Shown event is one option, but the constructor is often an even better option, it just depends on what you are REALLY doing, not just the contrived example. After all, at the time of the load event, you should never have a reason to check the contents of a textbox, because you would know what the textbox holds.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|