Results 1 to 4 of 4

Thread: Difficulty accessing 'disposed' form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Unhappy Difficulty accessing 'disposed' form

    I'm having a strange difficulty. Here is the order of events:

    1. User presses button on form1
    2. New Form object is created for from 2
    3. New form object '.show' command is fired
    4. Form 2 opens correctly
    5. In the Load Event of Form 2, the form attempts to do various things in a try, catch, finally clause
    6. On catch, I close form2 down '.close' as it cannot be used as something has gone wrong with the execution


    However, my problem is this. It is acceptable for things to go wrong on the from 2 load event which is why I have implemented the try - catch functionality. However, when I close the form2 down as a result of the problem, a real error ocurrs 'Cannot access a disposed object' and the VB IDE refers me to Form1's '.show' event for Form2.

    It is almost as if the form hasn't fully 'shown' before I am closing it down. Does anyone have any idea what the problem is here? Do I need to close down Form2 ourside of it's Load event handler so that Form1 thinks it has loaded correctly?

    Cheers!

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Difficulty accessing 'disposed' form

    I would assume this is because there is a chain of events to be raised when a form is shown, as can be seen here. I would do one of two things:
    1. Rethink the design of this solution. Can whatever it is you are doing in the Load eventhandler of Form2 be done before the form is created in the first place? This would avoid the entire process of creating Form2 and attempting to display it only to close it down instantly.
    2. Use the Shown event instead of the Load event and see if that works better.

    I would go with soution #1 any day. It would make for a better design.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: Difficulty accessing 'disposed' form

    Quote Originally Posted by Atheist View Post
    I would assume this is because there is a chain of events to be raised when a form is shown, as can be seen here. I would do one of two things:
    1. Rethink the design of this solution. Can whatever it is you are doing in the Load eventhandler of Form2 be done before the form is created in the first place? This would avoid the entire process of creating Form2 and attempting to display it only to close it down instantly.
    2. Use the Shown event instead of the Load event and see if that works better.

    I would go with soution #1 any day. It would make for a better design.
    Thanks for that Atheist, exactly what I was look for that chain of events. What I have done is set a boolean variable within the load event handler if the form needs to be closed after it is shown or not.

    Then, in the shown event handler, I can check the status of that boolean variable and either close the form or do nothing. Has worked a treat.

    Shown events evidently fire once the form has been fully loaded.

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

    Re: Difficulty accessing 'disposed' form

    Do you have to do that work in the Load event? I would prefer to move it earlier into the constructor. After all, creating and showing the form is a two step process. The first is creation, the second is showing. You are aborting the showing in the Load event, but it would be better to abort the very need for showing by moving that initialization back to creation, if possible. Then the steps would look like this:
    Code:
    Dim nf As New Form2 'Creation occurs here.
    
    if nf.IsGood Then
     nf.Show 'Display here
    End If
    Of course, that would require implementing an IsGood property, but that's minor, as you are already doing that with your Boolean.
    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