Cannot access disposed object named (Splash Screen related problem) 
Author Message
 Cannot access disposed object named (Splash Screen related problem)

Hello,

The following code successfully displays a splash screen and loads a second
form.  The method I am using is for the splash form to hide itself, load the
second form, and have the entire program unload when the exit button is
clicked on the second form.  (The splash screen is programmed to unload via
a timer which I have set through the property controls.)  When the program
terminates, it ends with an exception error:

"Cannot access disposed object named 'Form2'."

Posted below is the relevant code from the splash form.  Form2 just does an
me.close to finish up.  How can I fix this?  Thank you!

Public Class Splash
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

       Dim Form2 As New Form2()

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

        Me.Hide()
        Form2.Show()

    End Sub

    Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
MyBase.Load

    End Sub

End Class



Wed, 14 Dec 2005 00:55:12 GMT  
 Cannot access disposed object named (Splash Screen related problem)

Quote:
> The following code successfully displays a splash screen and loads a
> second form.  The method I am using is for the splash form to hide
> itself, load the second form, and have the entire program unload when
> the exit button is clicked on the second form.  (The splash screen is
> programmed to unload via a timer which I have set through the
> property controls.)  When the program terminates, it ends with an
> exception error:

> "Cannot access disposed object named 'Form2'."

> Posted below is the relevant code from the splash form.  Form2 just
> does an me.close to finish up.  How can I fix this?  Thank you!

> Public Class Splash
>     Inherits System.Windows.Forms.Form

> #Region " Windows Form Designer generated code "

>        Dim Form2 As New Form2()

>     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
> As
> System.EventArgs) Handles Timer1.Tick

>         Me.Hide()
>         Form2.Show()

>     End Sub

>     Private Sub Splash_Load(ByVal sender As System.Object, ByVal e
> As
> System.EventArgs) Handles
> MyBase.Load

>     End Sub

> End Class

Add
    Timer1.Enabled = False
after showing Form2.

Otherwise the following happens: When closing Form2, it is disposed. After
that, the timer of Form1 still ticks and Form2.Show is called again. As the
variable Form2 still points to the same instance, the one that is disposed
now, Form2.Show leads to this exception.

--
Armin



Wed, 14 Dec 2005 01:37:30 GMT  
 Cannot access disposed object named (Splash Screen related problem)

Quote:

> Add
>     Timer1.Enabled = False
> after showing Form2.

> Otherwise the following happens: When closing Form2, it is disposed. After
> that, the timer of Form1 still ticks and Form2.Show is called again. As
the
> variable Form2 still points to the same instance, the one that is disposed
> now, Form2.Show leads to this exception.

Armin, thank you!  That got rid of my exception error.  My only problem now
is that the program will not stop.  The windows close and nothing is seen,
but Visual Studio is still in run mode.  Where in my splash screen must I
stop the program?  I really don't understand the flow.

Thanks again!



Wed, 14 Dec 2005 04:28:39 GMT  
 Cannot access disposed object named (Splash Screen related problem)

Quote:

> > Add
> >     Timer1.Enabled = False
> > after showing Form2.

> > Otherwise the following happens: When closing Form2, it is
> > disposed. After that, the timer of Form1 still ticks and Form2.Show
> > is called again. As
> the
> > variable Form2 still points to the same instance, the one that is
> > disposed now, Form2.Show leads to this exception.

> Armin, thank you!  That got rid of my exception error.  My only
> problem now is that the program will not stop.  The windows close and
> nothing is seen, but Visual Studio is still in run mode.  Where in my
> splash screen must I stop the program?  I really don't understand the
> flow.

I knew this question will occur. ;-)

You've only hidden the startup form. The application won't exit until the
startup form is closed. One solution (there are others):

   Dim Form2 As New Form2

   Shared Sub main()
      Call (New Form1).ShowDialog()
      Application.Run(New Form2)
   End Sub

   Private Sub Timer1_Tick( _
      ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles Timer1.Tick

      Me.Close()

   End Sub

--
Armin



Wed, 14 Dec 2005 05:01:58 GMT  
 Cannot access disposed object named (Splash Screen related problem)

Quote:

> I knew this question will occur. ;-)

> You've only hidden the startup form. The application won't exit until the
> startup form is closed. One solution (there are others):

>    Dim Form2 As New Form2

>    Shared Sub main()
>       Call (New Form1).ShowDialog()
>       Application.Run(New Form2)
>    End Sub

>    Private Sub Timer1_Tick( _
>       ByVal sender As System.Object, ByVal e As System.EventArgs) _
>       Handles Timer1.Tick

>       Me.Close()

>    End Sub

Oh Gawd!  Now I would have to recode the whole form.  There just has to be
some simpler way.  Oh how I long for the days of DOS and BASIC PDS!  :)


Wed, 14 Dec 2005 08:20:52 GMT  
 Cannot access disposed object named (Splash Screen related problem)

Quote:
> Oh Gawd!  Now I would have to recode the whole form.  There just has to be
> some simpler way.  Oh how I long for the days of DOS and BASIC PDS!  :)

Come hell or high water I will figure this out and post the answer tomorrow
afternoon.  And forever it shall remain on Google for all to see!


Fri, 16 Dec 2005 11:41:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Cannot access a disposed object in frmLogin

2. Cannot dispose wrapped Excel Library object - Please Help

3. Supress Access Splash Screen in Access 2000

4. Splash Screen Delay] Note on splashes

5. Cannot access a disposed object named "frmMain".

6. Additional information: Cannot access a disposed object named "ListBox".

7. InternetExplorer object shows splash screen when instantiated!

8. InternetExplorer object - supress splash screen!

9. InternetExplorer object - supress splash screen

10. InternetExplorer object shows splash screen when instantiated!

11. Unable to access disposed object Exception, anyone else?

12. serious problem - need help on disposed object error

 

 
Powered by phpBB® Forum Software