Question Form Loading problem!!!!

the_wayman

Member
Joined
Jan 9, 2009
Messages
5
Programming Experience
5-10
This a code to load a form as a child form in a mdiparent form every thing works fine. but when i try load a already loaded form it gives an error

Cannot access a disposed object. Object name: '<Form Name>'.
Here is the code....

Private Sub LoadPage(ByVal formToLoad As Form, ByVal Icon As Image)

If LoadDone = True Then 'LoadDone= false when no forms r loaded

frm.Close()

End If



pnBack.ValuesPrimary.Image = Icon
pnBack.ValuesPrimary.Heading = (formToLoad.Text)
frm = formToLoad
frm.MdiParent = Me
Me.pnBack.Panel.Controls.Add(frm)
frm.Width = Me.pnBack.Panel.Width
frm.Height = Me.pnBack.Panel.Height

frm.Show()

LoadDone = True
End Sub

Please i need some help urgently.
Thanks A lot in advance.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,464
Location
Norway
Programming Experience
10+
You can check the IsDisposed property to check this, used when you have a variable reference and don't know if user has closed the form. If it is disposed you have to create a new form instance. If you only need one instance of a designed form at any given time you can use the default forms feature, just reference the form by type name (for example MapForm), instead of creating a new instance (New MapForm).
 

the_wayman

Member
Joined
Jan 9, 2009
Messages
5
Programming Experience
5-10
thanks for the prompt answer.
the sub works if i the fromtoload changes but if i want to RELOAD the same from again(i need to) it gives and error.

PS. it does not give any error if i suppose LoadPage(from1) then LoadPage(Form2) then again LoadPage(Form1) But LoadPage(From1) then LoadPage(Form1) give the error.

Thanks again
 
Top Bottom