Author Topic: Modeless Form  (Read 2897 times)

0 Members and 1 Guest are viewing this topic.

poncelet

  • Guest
Modeless Form
« on: August 25, 2011, 11:45:07 AM »
When i close the form and enter the command again i got an error. Why?
Code: [Select]
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

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Modeless Form
« Reply #1 on: August 25, 2011, 12:07:33 PM »
What does the error message say?
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Ken Alexander

  • Newt
  • Posts: 61
Re: Modeless Form
« Reply #2 on: August 25, 2011, 12:18:27 PM »
The form is Disposed.  Create a new instance each time or override the Forms OnFormClosing to prevent the form from closing/disposing.
Ken Alexander

poncelet

  • Guest
Re: Modeless Form
« Reply #3 on: August 25, 2011, 12:19:06 PM »
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.

Jeff H

  • Needs a day job
  • Posts: 6143
Re: Modeless Form
« Reply #4 on: August 25, 2011, 12:22:00 PM »
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
 
Quote

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 event and setting the Cancel property of the CancelEventArgs 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
 
Not the best way of doing it but a quick fix
 
 
 
**********************************EDIT******************************
That just does not feel right.
Quote
    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

 
 
 
« Last Edit: August 25, 2011, 12:32:27 PM by Jeff H »

poncelet

  • Guest
Re: Modeless Form
« Reply #5 on: August 25, 2011, 12:25:57 PM »
Thank you all. I will try it.

poncelet

  • Guest
Re: Modeless Form
« Reply #6 on: August 25, 2011, 12:41:09 PM »
Jeff it is working

Ken Alexander

  • Newt
  • Posts: 61
Re: Modeless Form
« Reply #7 on: August 25, 2011, 12:47:16 PM »

Quote

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

Code: [Select]
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

Ken Alexander

poncelet

  • Guest
Re: Modeless Form
« Reply #8 on: August 25, 2011, 12:56:26 PM »
Code: [Select]
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

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Modeless Form
« Reply #9 on: August 25, 2011, 01:19:54 PM »
That just does not feel right.
That's what she said.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Jeff H

  • Needs a day job
  • Posts: 6143
Re: Modeless Form
« Reply #10 on: August 25, 2011, 01:23:18 PM »
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.
Code: [Select]
        <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

 
 
 
« Last Edit: August 25, 2011, 06:17:51 PM by Jeff H »