<CommandMethod("crane")> _ Sub Crane() Application.ShowModelessDialog(myForm) End Sub
Show it only happens after I've open the form once. When I close it and try to re-open it it fails to open.
How do I dispose of VB.NET objects... and what am I dealing with here?
Solved! Go to Solution.
Okay So I solved it I think?
By placing the Constructor inside the procedure
It use to be Public Shared myForm as new frmBatchProcess
Public Class Crawler <CommandMethod("crane")> _ Sub Crane() dim myForm As New frmBatchProcess 'The Instance of the Batch Crawler Window If myForm.IsDisposed = True Then MsgBox("Form is Disposed of, Please Restart Autocad and Try Again") Else Application.ShowModelessDialog(myForm) End If End Sub
Well, whether you solve it or not would be dependnet on how do you want to use the modeless form and if you know windows form's closing behaviour when being shown as modeless and modal form.
1. Windows form closing behaves differently when being shown as modeless and modal. When as modelsss form, calling Form.Close() or clicking "x" on the form, actually disposes the form (truly closed), while whne bing shown as modal form (dialog box, usually), calling Form.Close()/clicking "x" is equal to calling Form.Hide() or Form.Visible=False. The form is not disposed.
So, your previous error must be due to your code is calling Form.Close() or user clicking "x", which results in the form being disposed.
2. Yes, when you "New" the form each time your comment method is used as your code showing, a new instance of the form is created, regardless if you have closed previously opened form or not. It is completely not necessary to test
If myForm.IsDisposed Then...
because the form is just created (unless you have complicated code in the form constructor that may dispose itself due to some initializing condition not being met).
3. With your code, if you run your commend method more than once without explicitly closing opened form, you would end up with multiple modeless form opening, which may or may not be what you want to. Each time you close a form, it is disposed, data shown on the form is lost. When you "New" the form again, you need to reload data to the form, or even worse, you may have to try to restore the data shown on a closed form. Your code alos has the potential risk of shown mutiple forms with different data from drawing when calling your command method more than once against the same document in different time, because the drawing could be changed by user.
4. Usually, it is desired to use a singleton modeless form. That is why you may want to declare Shared myForm As frmBacthprocess at class level. To make it singleton, you handle form's Closing event and set e.Cancel=True, and set Me.Visible=False, so that the form.Close() method (or clicking "x" would not close the form, but hide the form instead.
5. Your form is named as frmBatchProcess, which implies that you donot want to run multiple forms. if the batch process means processing multiple drawing files, you would want to set CommandFlags.Session flag in your command method and you certainly want to use a singleton modeless form.
AWESOME! Thanks for the clarification.
Norman you're a true programmer I can tell Thanks!
Appreciate your blog as well.