[Solved] Windows Application Error: Cannot access a disposed object

Last Reply 4 months ago By dharmendr

Posted 4 months ago

Hello Everyone, 

I have this error that accurs in my vb.net forms, when I press the button once it shows me the panel and if I pres the button twice at the same time I get this error : Cannot access a disposed object.

This is the code :

   Private Sub OpenChildForm(childForm As Form)
        'Open only Form
        If currentChildForm IsNot Nothing Then
            currentChildForm.Close()
        End If
        currentChildForm = childForm
        'end
        childForm.TopLevel = False
        childForm.FormBorderStyle = FormBorderStyle.None
        childForm.Dock = DockStyle.Fill
        PanelDesktop.Controls.Add(childForm)
        PanelDesktop.Tag = childForm
        childForm.BringToFront()
        childForm.Show()
        lblFormTitle.Text = childForm.Text

    End Sub

and I get the error in this line :

 childForm.Show()

Thank you,  Best Regards

Posted 4 months ago

Hi elvisidrizi1,

Try using Hide method instead of Close.

VB.Net

Private Sub OpenChildForm(childForm As Form)
    'Open only Form
    If currentChildForm IsNot Nothing Then
        currentChildForm.Hide()
    End If
    currentChildForm = childForm
    'end
    childForm.TopLevel = False
    childForm.FormBorderStyle = FormBorderStyle.None
    childForm.Dock = DockStyle.Fill
    PanelDesktop.Controls.Add(childForm)
    PanelDesktop.Tag = childForm
    childForm.BringToFront()
    childForm.Show()
    lblFormTitle.Text = childForm.Text
End Sub