TabControl and Form.Dispose() - Strange Behaviour

T

theintrepidfox

Dear Group

I wonder if you can help me with the following. I have a form(Form2)
that contains a tabcontrol. If I click 'Tab2' Form3 should be displayed
and 'Form2' closed. The instruction to close Form2 is in the Load event
of Form3 but it actually doesn't matter where I put it. Every time I
get this error:

An unhandled exception of type 'System.ObjectDisposedExceptio­­n'
occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named
"TabControl".

Interestingly, If I have a button on Form3 to close the Form, Form2
closes just fine. However I want to close Form2 when Form3 has loaded.

Also, if I place a button on Form to to open Form3 and don't use the
SelectedIndexCHanged TabControlEvent, I also don't get this error. It's
almost as it's looking for the TabControl when the error comes up.

clsForms is just to keep track of my forms instances.

Any ideas. Please let me know.
Thanks very much for your help and efforts and please accept my
apologies for posting all the code but thought it might help to
undertsand.

Martin


x-x-x--x-x-x-x-x-x-x-x-x- FORM1 START


Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim frm2 As New Form2


frm2.MdiParent = Me


frm2.Show()
clsForms.Form2 = frm2


End Sub


x-x-x--x-x-x-x-x-x-x-x-x- FORM1 END


x-x-x--x-x-x-x-x-x-x-x-x- FORM2 START


Private Sub TabControl1_SelectedIndexChang­­ed(ByVal sender As
Object,
ByVal e As System.EventArgs) Handles
TabControl1.SelectedIndexChang­­ed


Select Case TabControl1.SelectedTab.Name
Case "TabPage2"
Dim frm3 As New Form3


frm3.MdiParent = clsForms.Form1


frm3.Show()
clsForms.Form3 = frm3


End Select


x-x-x--x-x-x-x-x-x-x-x-x- FORM2 END


x-x-x--x-x-x-x-x-x-x-x-x- FORM3 START


Private Sub Form3_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load


clsForms.Form2.Close()


End Sub


x-x-x--x-x-x-x-x-x-x-x-x- FORM3 END


x-x-x--x-x-x-x-x-x-x-x-x- clsForms START


Public Class clsForms


Private Shared frm1 As Form1
Private Shared frm2 As Form2
Private Shared frm3 As Form3


Public Shared Property Form1() As Form1
Get
Return frm1
End Get
Set(ByVal Value As Form1)
frm1 = Value
End Set
End Property


Public Shared Property Form2() As Form2
Get
Return frm2
End Get
Set(ByVal Value As Form2)
frm2 = Value
End Set
End Property


Public Shared Property Form3() As Form3
Get
Return frm3
End Get
Set(ByVal Value As Form3)
frm3 = Value
End Set
End Property


End Class


x-x-x--x-x-x-x-x-x-x-x-x- clsForms END
 
K

Ken Tucker [MVP]

Hi,

I dont see where you ever create your forms try changing all the
form properties in your class clsforms to something like this.

Public Shared Property Form1() As Form1
Get
If frm1 is nothing then frm1=new form1
Return frm1
End Get
Set(ByVal Value As Form1)
frm1 = Value
End Set
End Property

Ken
----------------------------

Dear Group

I wonder if you can help me with the following. I have a form(Form2)
that contains a tabcontrol. If I click 'Tab2' Form3 should be displayed
and 'Form2' closed. The instruction to close Form2 is in the Load event
of Form3 but it actually doesn't matter where I put it. Every time I
get this error:

An unhandled exception of type 'System.ObjectDisposedExceptio­­n'
occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named
"TabControl".

Interestingly, If I have a button on Form3 to close the Form, Form2
closes just fine. However I want to close Form2 when Form3 has loaded.

Also, if I place a button on Form to to open Form3 and don't use the
SelectedIndexCHanged TabControlEvent, I also don't get this error. It's
almost as it's looking for the TabControl when the error comes up.

clsForms is just to keep track of my forms instances.

Any ideas. Please let me know.
Thanks very much for your help and efforts and please accept my
apologies for posting all the code but thought it might help to
undertsand.

Martin


x-x-x--x-x-x-x-x-x-x-x-x- FORM1 START


Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim frm2 As New Form2


frm2.MdiParent = Me


frm2.Show()
clsForms.Form2 = frm2


End Sub


x-x-x--x-x-x-x-x-x-x-x-x- FORM1 END


x-x-x--x-x-x-x-x-x-x-x-x- FORM2 START


Private Sub TabControl1_SelectedIndexChang­­ed(ByVal sender As
Object,
ByVal e As System.EventArgs) Handles
TabControl1.SelectedIndexChang­­ed


Select Case TabControl1.SelectedTab.Name
Case "TabPage2"
Dim frm3 As New Form3


frm3.MdiParent = clsForms.Form1


frm3.Show()
clsForms.Form3 = frm3


End Select


x-x-x--x-x-x-x-x-x-x-x-x- FORM2 END


x-x-x--x-x-x-x-x-x-x-x-x- FORM3 START


Private Sub Form3_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load


clsForms.Form2.Close()


End Sub


x-x-x--x-x-x-x-x-x-x-x-x- FORM3 END


x-x-x--x-x-x-x-x-x-x-x-x- clsForms START


Public Class clsForms


Private Shared frm1 As Form1
Private Shared frm2 As Form2
Private Shared frm3 As Form3


Public Shared Property Form1() As Form1
Get
Return frm1
End Get
Set(ByVal Value As Form1)
frm1 = Value
End Set
End Property


Public Shared Property Form2() As Form2
Get
Return frm2
End Get
Set(ByVal Value As Form2)
frm2 = Value
End Set
End Property


Public Shared Property Form3() As Form3
Get
Return frm3
End Get
Set(ByVal Value As Form3)
frm3 = Value
End Set
End Property


End Class


x-x-x--x-x-x-x-x-x-x-x-x- clsForms END
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top