473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

TabControl and Form.Dispose() - Strange Behaviour

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

Nov 21 '05 #1
1 2155
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
----------------------------

<th************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ken Lee | last post by:
Hi all, I'm sort of very new to C# and haven't programmed in OO for a while. I'm trying to override the backgroundimage to the tabcontrol but it just doesn't seem to want to do it, can someone...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
0
by: Gianluca | last post by:
Hi All, i'm experencing a frustrating and strange behaviaour : I created a Windows project in VB.NET 2003 with a form and then I added a TabControl on the form, and added 5 TabPages on...
9
by: Michael Turner | last post by:
Hi Guys Having problem with the tab control, I need to set the background color to something different than the standard, I have found code on the web and now can redraw the tabpage buttons so...
18
by: Jan Nielsen | last post by:
Hi I have a main form showing personal information (name, address etc.) bound to a dataset. One of the pieces of information is the Group the person belongs to. This is selected from a combo box....
0
by: EMW | last post by:
I am busy with a program for my pocket pc. but since it is still compact famework, I'll post this here too, because I'm very much in need for some help with this: I have one form which is my...
8
by: Gregory A Greenman | last post by:
I have a minor, but somewhat frustrating, problem. In a program I'm working on, I've got a tabcontrol that contains several other controls, a few of which are comboboxes. In the IDE, one of the...
8
by: nirdeshonline | last post by:
Hi, I have added a simple listbox in windows form under c# 2.0. It contains a collection of approx 10 strings as list items. Now when i resize the form whole listbox flickers. Please tell me...
2
sgeklor
by: sgeklor | last post by:
Hi guys, I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.