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

elvisidrizi1
 
on Sep 27, 2020 11:44 PM
Sample_103398.zip
712 Views

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 28, 2020 06:01 AM

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