Cannot access a disposed object in instance VB.net

i have problem with my project, i have 2 form, 1 form with MDI container = true and 1 form is child form. i have code in parent form like below

   Public Class frmInduk
    Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
    Private Sub DataBarangToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataBarangToolStripMenuItem.Click

        With afrmDaftarBarang
            .MdiParent = Me
            'Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
            .Show()
            .Focus()

        End With
    End Sub
End Class

and than, in my child form i have code like below

Public Class frmDaftarBarang
Private Shared anInstance As frmDaftarBarang
Public Shared ReadOnly Property instance() As frmDaftarBarang
    Get
        If anInstance Is Nothing Then
            anInstance = New frmDaftarBarang

        End If
        Return anInstance

    End Get
End Property

End Class

when i run this project, it no problem . my project can run very well. but the truble come when i call frmDataBarang, and then i close, but when i call frmDataBarang again i get some error lice picture bellow

i hope anybody can help me :(

Answers


You are trying to use the Singleton pattern, but you did it imperfectly. An MDI child form can be created more than once, you will have to set the singleton instance back to Nothing when it gets destroyed. Do so with the FormClosing event:

Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
    anInstance = Nothing
    MyBase.OnFormClosed(e)
End Sub

You will also need to do something reasonable when an existing instance is displayed again. It might not be in the right state. This code really belongs in the MDI parent, but you can limp along with this:

    Get
        If anInstance Is Nothing Then
            anInstance = New frmDaftarBarang
        Else
            If anInstance.WindowState = FormWindowState.Minimized Then
                anInstance.WindowState = FormWindowState.Normal
            End If
        End If
        Return anInstance

Do note that this gets ugly when you use the singleton to do things other than child activation. It is really rather a bad idea to use the pattern. It is fine on your MdiParent, there can only ever be one parent and when it gets closed then the program terminates anyway. But those same rules don't apply to a child. Move child instance management code to the parent class, that's where it belongs.


Need Your Help

Can we execute multiple commands in the same command prompt launched by python

python

There is an excel file which contains the paths of multiple scenarios. I am using os.system(command) in a for loop. In each iteration, a path is read from excel file and executes the scenario for t...

Java flexjson Serializing composed JSON objects

java json serialization flexjson

I have a Java class and I want to generate a JSON string from an object of the class. However, the members of the class are as follows: