Cannot access a disposed object in instance VB.net -
i have problem project, have 2 form, 1 form mdi container = true
, 1 form child form. have code in parent form below
public class frminduk dim afrmdaftarbarang frmdaftarbarang = frmdaftarbarang.instance private sub databarangtoolstripmenuitem_click(byval sender system.object, byval e system.eventargs) handles databarangtoolstripmenuitem.click afrmdaftarbarang .mdiparent = me 'dim afrmdaftarbarang frmdaftarbarang = frmdaftarbarang.instance .show() .focus() end end sub end class
and than, in child form have code below
public class frmdaftarbarang private shared aninstance frmdaftarbarang public shared readonly property instance() frmdaftarbarang if aninstance nothing aninstance = new frmdaftarbarang end if return aninstance end end property end class
when run project, no problem . project can run well. truble come when call frmdatabarang
, , close, when call frmdatabarang
again error lice picture bellow
i hope can me :(
you trying use singleton pattern, did imperfectly. mdi child form can created more once, have set singleton instance nothing when gets destroyed. formclosing event:
protected overrides sub onformclosed(byval e formclosedeventargs) aninstance = nothing mybase.onformclosed(e) end sub
you need reasonable when existing instance displayed again. might not in right state. code really belongs in mdi parent, can limp along this:
if aninstance nothing aninstance = new frmdaftarbarang else if aninstance.windowstate = formwindowstate.minimized aninstance.windowstate = formwindowstate.normal end if end if return aninstance
do note gets ugly when use singleton things other child activation. rather bad idea use pattern. fine on mdiparent, there can ever 1 parent , when gets closed program terminates anyway. same rules don't apply child. move child instance management code parent class, that's belongs.
Comments
Post a Comment