By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,360 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,360 IT Pros & Developers. It's quick & easy.

What's wrong with this code?

P: n/a
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button. I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies

P: n/a
On Tue, 26 Oct 2004 09:49:03 -0700, vvenk wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.
<snip>
This works fine except when I close Form2 and click on the command button. I
get an exception:


When you close a form it is disposed. If you need to use the form again,
call it Hide method instead of the Close method.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #2

P: n/a
The problem you are having is that formForm2 variable in Form1 doesn't get
set to Nothing when the Form2 closes.

You'll need to handle the close event in Form2 that needs to set this
variable to nothing.

HTH,
Regards
Simon Jefferies
mailto:simon[nospam]@cooltoolsonline.co.uk
-- remove [nospam] to email me --
"vvenk" <vv***@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will
open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button.
I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred
in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #3

P: n/a
On Tue, 26 Oct 2004 09:49:03 -0700, vvenk wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.
<snip>
This works fine except when I close Form2 and click on the command button. I
get an exception:


When you close a form it is disposed. If you need to use the form again,
call it Hide method instead of the Close method.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #4

P: n/a
The problem you are having is that formForm2 variable in Form1 doesn't get
set to Nothing when the Form2 closes.

You'll need to handle the close event in Form2 that needs to set this
variable to nothing.

HTH,
Regards
Simon Jefferies
mailto:simon[nospam]@cooltoolsonline.co.uk
-- remove [nospam] to email me --
"vvenk" <vv***@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will
open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button.
I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred
in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #5

P: n/a
set a public variable F2_Open in a module

This goes in form1:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If module1.F2_Open = False Then
Dim F2 As New Form2()
F2.Show()
module1.F2_Open = True
End If

In the Closed event of the Form2 class, reset F2_Open to false. You might
also set the TopMost property of Form2 to true, so it does not appear to
vanish if the user clicks elsewhere.

www.charlesfarriersoftware.com

"vvenk" wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button. I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #6

P: n/a
set a public variable F2_Open in a module

This goes in form1:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If module1.F2_Open = False Then
Dim F2 As New Form2()
F2.Show()
module1.F2_Open = True
End If

In the Closed event of the Form2 class, reset F2_Open to false. You might
also set the TopMost property of Form2 to true, so it does not appear to
vanish if the user clicks elsewhere.

www.charlesfarriersoftware.com

"vvenk" wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button. I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #7

P: n/a
Hi vvenk,

try the next code:

[FORM1]

Public Class Form1
Inherits System.Windows.Forms.Form

Private formForm2 As New Form2

Private Shared m_ActiveForm As Boolean

Public Shared Property ActualForm() As Boolean
Get
ActualForm = m_ActiveForm
End Get
Set(ByVal Value As Boolean)
m_ActiveForm = Value
End Set
End Property
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ActualForm Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
ActualForm = True
End Sub
End Class
[FORM2]
Public Class Form2
Inherits System.Windows.Forms.Form
....
Private Sub Form2_Closed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
Form1.ActualForm = False
End Sub
End Class

I hope that helps.

Best Regards,

Jorge Serrano Pérez
MVP VB.NET


"vvenk" wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button. I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #8

P: n/a
Hi vvenk,

try the next code:

[FORM1]

Public Class Form1
Inherits System.Windows.Forms.Form

Private formForm2 As New Form2

Private Shared m_ActiveForm As Boolean

Public Shared Property ActualForm() As Boolean
Get
ActualForm = m_ActiveForm
End Get
Set(ByVal Value As Boolean)
m_ActiveForm = Value
End Set
End Property
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ActualForm Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
ActualForm = True
End Sub
End Class
[FORM2]
Public Class Form2
Inherits System.Windows.Forms.Form
....
Private Sub Form2_Closed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
Form1.ActualForm = False
End Sub
End Class

I hope that helps.

Best Regards,

Jorge Serrano Pérez
MVP VB.NET


"vvenk" wrote:
Hello:

I have two forms, Form1 and Form2. Form1 has a command button that will open
Form2. There can only be one Form2 open at any time.

Here's my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Private formForm2 As New Form2
+ Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If formForm2 Is Nothing Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
End Sub
End Class
This works fine except when I close Form2 and click on the command button. I
get an exception:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

How do I solve this issue?

Thanks for your help.

Venkat

Nov 21 '05 #9

P: n/a
If formForm2 Is Nothing OrElse formForm2.IsDisposed Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
Nov 21 '05 #10

P: n/a
If formForm2 Is Nothing OrElse formForm2.IsDisposed Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
Nov 21 '05 #11

P: n/a
Rulin:

Of course, there is more than one way to skin the cat. But your solution is
by far the most elegant.

Thanks a lot.

"Rulin Hong" wrote:
If formForm2 Is Nothing OrElse formForm2.IsDisposed Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()

Nov 21 '05 #12

P: n/a
Rulin:

Of course, there is more than one way to skin the cat. But your solution is
by far the most elegant.

Thanks a lot.

"Rulin Hong" wrote:
If formForm2 Is Nothing OrElse formForm2.IsDisposed Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.