Results 1 to 5 of 5

Thread: [RESOLVED] Cannot access disposed form?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Resolved [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.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    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.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width