What's wrong with this code?

G

Guest

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
 
S

Simon Jefferies

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 --
 
C

Chris Dunaway

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.

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.
 
G

Guest

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
 
G

Guest

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
 
G

Guest

If formForm2 Is Nothing OrElse formForm2.IsDisposed Then
formForm2 = New Form2
End If
formForm2.Show()
formForm2.Activate()
 
G

Guest

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.
 

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