TheSwamp
Code Red => .NET => Topic started by: poncelet on August 25, 2011, 11:45:07 AM
-
When i close the form and enter the command again i got an error. Why?
Public Class Class1
Public f As MainForm
<CommandMethod("run")> _
Public Sub run()
If f Is Nothing Then
f = New MainForm
Application.ShowModelessDialog(f)
Else
f.Visible = True
End If
End Sub
End Class
-
What does the error message say?
-
The form is Disposed. Create a new instance each time or override the Forms OnFormClosing to prevent the form from closing/disposing.
-
unhandled exception has occured in a component in your application.
If you click Continue, the application will ignore this error and attempt to continue.
Exception has been thrown by the target of an invocation.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MainForm'. blah blah blah...
Well one solution is to throw the pc off the window or throw the windows off the pc.
PS: In two weeks i started programming in .net i have seen more errors than 7 years programming in vba.
-
Welcome to the Swamp!
I am not totally sure since AutoCAD handles some of the operation but if the form is not opened using ShowDialog() or part of a MDI when Close is called all resources are disposed
When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing (http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx) event and setting the Cancel (http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx) property of the CancelEventArgs (http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.aspx) passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx (http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx)
Not the best way of doing it but a quick fix
**********************************EDIT******************************
That just does not feel right.
Public f As Form1
<CommandMethod("run")> _
Public Sub run()
If f Is Nothing Then
f = New Form1
Application.ShowModelessDialog(f)
ElseIf f.IsDisposed Then
f = New Form1
Application.ShowModelessDialog(f)
Else
f.Visible = True
End If
End Sub
-
Thank you all. I will try it.
-
Jeff it is working
-
That just does not feel right.
Public f As Form1
<CommandMethod("run")> _
Public Sub run()
If f Is Nothing Then
f = New Form1
Application.ShowModelessDialog(f)
ElseIf f.IsDisposed Then
f = New Form1
Application.ShowModelessDialog(f)
Else
f.Visible = True
End If
End Sub
Maybe.....
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
MyBase.OnFormClosing(e)
If Not e.Cancel Then
e.Cancel = True
Me.Visible = False
End If
End Sub
-
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
MyBase.OnFormClosing(e)
If Not e.Cancel Then
e.Cancel = True
Me.Visible = False
End If
End Sub
This works even better
-
That just does not feel right.
That's what she said.
-
A thing to keep in mind is since you are declaring it in a class with CommandAttributes AutoCAD will create a new class for each Document.
So if you open it one document and create another one and call run again then you will have 2 instances of the form open.
You could make f shared so each class will point to the same f in memory.
Another way but propbably best to use the way Ken suggested.
The suggestion Ken made will just 2 forms if you do not make f shared but this way will error if you do not make it shared.
<CommandMethod("run")> _
Public Sub run()
Dim isOpen As Boolean = False
For Each frm As System.Windows.Forms.Form In System.Windows.Forms.Application.OpenForms
If (frm.Name = f.Name) Then
isOpen = True
Exit For
End If
Next
If Not isOpen Then
f = New Form1
Application.ShowModelessDialog(f)
Else
f.Visible = True
End If
End Sub