Difficulty accessing 'disposed' form
I'm having a strange difficulty. Here is the order of events:
- User presses button on form1
- New Form object is created for from 2
- New form object '.show' command is fired
- Form 2 opens correctly
- In the Load Event of Form 2, the form attempts to do various things in a try, catch, finally clause
- 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! :confused:
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.
Re: Difficulty accessing 'disposed' form
Quote:
Originally Posted by
Atheist
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. :)
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.