<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to handle multiple open documents gracefully in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082822#M76822</link>
    <description>&amp;gt;&amp;gt; So I dont need to worry about multiple instances &lt;BR /&gt;
&amp;gt;&amp;gt; of "MyApp" object being created. &lt;BR /&gt;
&lt;BR /&gt;
Well, not quite.&lt;BR /&gt;
&lt;BR /&gt;
The way it works is that if your command method is&lt;BR /&gt;
non-static (not shared), as is the case in your code,&lt;BR /&gt;
AutoCAD creates a seperate instance of your class, &lt;BR /&gt;
for each open document. So, if there are 3 open &lt;BR /&gt;
documents, there will be three instances of your &lt;BR /&gt;
"MyApp" class.&lt;BR /&gt;
&lt;BR /&gt;
That may be what is leading you to think that VB is &lt;BR /&gt;
automatically switching the events every time you&lt;BR /&gt;
assign a non-static/shared member variable that has &lt;BR /&gt;
the 'WithEvents', to another document. &lt;BR /&gt;
&lt;BR /&gt;
In fact, what you don't realize is that there is more &lt;BR /&gt;
than one instance of your class, and each of them is &lt;BR /&gt;
being used with a different document, so it gives the &lt;BR /&gt;
illusion that the event handlers are automatically &lt;BR /&gt;
added to the document assigned to the variable each &lt;BR /&gt;
time the variable's value is changed, but that's not&lt;BR /&gt;
what's going on at all.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JSPARK&gt; wrote in message news:5739332@discussion.autodesk.com...&lt;BR /&gt;
Yes, I have been wondering, and am unclear on when exactly "MyApp" is created. I would guess that NETLOAD does not create an instance of "MyApp". In order to execute the command method, an instance of "MyApp" is created... If thats the case then I would assume a second instance of "MyApp" is NOT created when the command is ran for a second time. So I dont need to worry about multiple instances of "MyApp" object being created. What about resetting mThisDrawing each time a new document becomes the active document? Will that make the same event handler work for the new active doc? (I guess I could do some tests to figure that out, but it sounds logical...) Am I on the right track here?&lt;/JSPARK&gt;</description>
    <pubDate>Sat, 06 Oct 2007 01:26:59 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2007-10-06T01:26:59Z</dc:date>
    <item>
      <title>How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082808#M76808</link>
      <description>I have a .NET app that is loaded with the NETLOAD command. I have a single command method that loads a custom control into a palette, and displays it. Whenever my code needs a reference to "ThisDrawing", "ThisAcad", "ThisDB" etc... it is gotten from a function that returns the needed reference. That said I have a few questions:&lt;BR /&gt;
&lt;BR /&gt;
Why do my event handlers (the app in general) only become active after I type the command for my command method? Why are they not functional after the NETLOAD command? Is my application object (the class that contains my command method and my withevents member declarations) only instantiated when a command method is called? &lt;BR /&gt;
&lt;BR /&gt;
Why does my app stop working when a new drawing is opened if the AcadApp, Document, Editor, Database etc.. references are gotten from a function? Example:&lt;BR /&gt;
&lt;BR /&gt;
Dim WithEvents ThisDrawing as Document = GetDoc()&lt;BR /&gt;
&lt;BR /&gt;
I think I am incorrectly assuming that each time my code uses ThisDrawing, it's the same as calling GetDoc(). Maybe I should explicitly set ThisDrawing = GetDoc() each time a new drawing becomes active?&lt;BR /&gt;
&lt;BR /&gt;
Any other tips on gracefully handling the transition from active document to active document will be much appreciated as well.&lt;BR /&gt;
&lt;BR /&gt;
TIA</description>
      <pubDate>Wed, 03 Oct 2007 03:20:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082808#M76808</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T03:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082809#M76809</link>
      <description>Concerning the event handlers, these must be initialized in the entrypoint of your app, not in the command method. The command method runs only when you call the corresponding command. The entrypoint runs when your app is loaded. That is the only thing that runs when an app is loaded.</description>
      <pubDate>Wed, 03 Oct 2007 08:57:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082809#M76809</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T08:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082810#M76810</link>
      <description>I suppose that if you've been using VBA and its&lt;BR /&gt;
'ThisDrawing' object, you might become confused&lt;BR /&gt;
by what you take for granted.&lt;BR /&gt;
&lt;BR /&gt;
In VBA, 'ThisDrawing' looks like a variable, but is&lt;BR /&gt;
in reality, more like a function that always returns &lt;BR /&gt;
the Active document in the editor.&lt;BR /&gt;
&lt;BR /&gt;
So in .NET, rather than assigning a variable to&lt;BR /&gt;
the result of your GetDoc() function (which I&lt;BR /&gt;
presume returns the active document), you must&lt;BR /&gt;
call the GetDoc() function when want the Active&lt;BR /&gt;
document (or to make it easier, you can add a&lt;BR /&gt;
property to your class called 'ThisDrawing', and&lt;BR /&gt;
in the 'getter' for it, just return the active doc).&lt;BR /&gt;
&lt;BR /&gt;
When you're working with palettes, you should&lt;BR /&gt;
never cache a document object in a variable. You &lt;BR /&gt;
should always reference the MdiActiveDocument&lt;BR /&gt;
property of the DocumentCollection directly, when&lt;BR /&gt;
an operation begins, and then use that value only&lt;BR /&gt;
for the duration of the operation. An 'operation'&lt;BR /&gt;
means when the user clicks a button on your&lt;BR /&gt;
user interface, and in response, you access the&lt;BR /&gt;
active document to do something.&lt;BR /&gt;
&lt;BR /&gt;
What is also not obvious about VBA's 'ThisDrawing', &lt;BR /&gt;
is that when you handle an event of 'ThisDrawing', &lt;BR /&gt;
the event is fired for every open document, not &lt;BR /&gt;
just the one that was active when the event was &lt;BR /&gt;
assigned.&lt;BR /&gt;
&lt;BR /&gt;
In .NET, things are not as simple or automatic as&lt;BR /&gt;
they are in VBA.&lt;BR /&gt;
&lt;BR /&gt;
In .NET a Document object represents a single &lt;BR /&gt;
Document open in the editor, that has its own&lt;BR /&gt;
events. When you add a handler for a Document&lt;BR /&gt;
event to a Document, the handler only receives &lt;BR /&gt;
notification for the event in the Document that &lt;BR /&gt;
you added the handler to. So, to handle the same&lt;BR /&gt;
document event in every document, you must&lt;BR /&gt;
add the handler for the event to every document&lt;BR /&gt;
manually.&lt;BR /&gt;
&lt;BR /&gt;
In order to do that, you handle the events of the &lt;BR /&gt;
DocumentCollection (Application.DocumentManager),&lt;BR /&gt;
like DocumentAdded, and DocumentToBeDestroyed,&lt;BR /&gt;
and in the handlers for those events, you must add&lt;BR /&gt;
or remove your document event handlers to the &lt;BR /&gt;
newly added, or about-to-be closed document.&lt;BR /&gt;
&lt;BR /&gt;
If your app is not loaded at startup, then you also&lt;BR /&gt;
have to iterate over the collection of currently open &lt;BR /&gt;
documents, and add document events handlers to &lt;BR /&gt;
each of them.&lt;BR /&gt;
&lt;BR /&gt;
There's sample code showing how to do this on my &lt;BR /&gt;
website, but its in C#. There may be someone here &lt;BR /&gt;
that has converted it to VB.NET.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JSPARK&gt; wrote in message news:5738738@discussion.autodesk.com...&lt;BR /&gt;
I have a .NET app that is loaded with the NETLOAD command. I have a single command method that loads a custom control into a palette, and displays it. Whenever my code needs a reference to "ThisDrawing", "ThisAcad", "ThisDB" etc... it is gotten from a function that returns the needed reference. That said I have a few questions:&lt;BR /&gt;
&lt;BR /&gt;
Why do my event handlers (the app in general) only become active after I type the command for my command method? Why are they not functional after the NETLOAD command? Is my application object (the class that contains my command method and my withevents member declarations) only instantiated when a command method is called? &lt;BR /&gt;
&lt;BR /&gt;
Why does my app stop working when a new drawing is opened if the AcadApp, Document, Editor, Database etc.. references are gotten from a function? Example:&lt;BR /&gt;
&lt;BR /&gt;
Dim WithEvents ThisDrawing as Document = GetDoc()&lt;BR /&gt;
&lt;BR /&gt;
I think I am incorrectly assuming that each time my code uses ThisDrawing, it's the same as calling GetDoc(). Maybe I should explicitly set ThisDrawing = GetDoc() each time a new drawing becomes active?&lt;BR /&gt;
&lt;BR /&gt;
Any other tips on gracefully handling the transition from active document to active document will be much appreciated as well.&lt;BR /&gt;
&lt;BR /&gt;
TIA&lt;/JSPARK&gt;</description>
      <pubDate>Wed, 03 Oct 2007 13:27:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082810#M76810</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T13:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082811#M76811</link>
      <description>Thank you both for your time. It has been very enlightening. A few more questions with some sample code this time…&lt;BR /&gt;
&lt;BR /&gt;
In a nutshell, this is “MyApp” object:&lt;BR /&gt;
&lt;BR /&gt;
Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;
Imports Autodesk.AutoCAD.Interop&lt;BR /&gt;
&lt;BR /&gt;
Public Class MyApp&lt;BR /&gt;
&lt;BR /&gt;
‘Private Members&lt;BR /&gt;
Private WithEvents mThisAcad as AcadApplication = Ctype(ThisAcad.AcadApplication,  AcadApplication)&lt;BR /&gt;
Private WithEvents mThisDrawing as AcadDocument = Ctype(ThisDrawing.AcadDocument,  AcadDocument)&lt;BR /&gt;
&lt;BR /&gt;
‘Public Properties&lt;BR /&gt;
Public Shared ReadOnly Property ThisAcad as Application&lt;BR /&gt;
Get&lt;BR /&gt;
Return Application.AcadApplication&lt;BR /&gt;
End Get&lt;BR /&gt;
End Property&lt;BR /&gt;
&lt;BR /&gt;
Public Shared ReadOnly Property ThisDrawing as Document&lt;BR /&gt;
Get&lt;BR /&gt;
Return Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
End Get&lt;BR /&gt;
End Property&lt;BR /&gt;
&lt;BR /&gt;
‘Methods&lt;BR /&gt;
(Autodesk.AutoCAD.Runtime.CommandMethod("Start", "Start", _ Autodesk.AutoCAD.Runtime.CommandFlags.Modal)&amp;gt;  _&lt;BR /&gt;
Public Sub Start()&lt;BR /&gt;
‘Create a new PaletteSet and a Palette. Add Palette to PaletteSet.&lt;BR /&gt;
‘Set PaletteSet.Visible = True&lt;BR /&gt;
EndSub&lt;BR /&gt;
&lt;BR /&gt;
‘Events&lt;BR /&gt;
Private Sub mThisDrawing_ObjectAdded(ByVal Obj As Object) Handles _ mThisDrawing.ObjectAdded&lt;BR /&gt;
‘Do Stuff&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
End Class&lt;BR /&gt;
&lt;BR /&gt;
If I were to reset mThisDrawing = Ctype(ThisDrawing.AcadDocument, AcadDocument) each time a new Document becomes the active document, would my ObjectAdded event now only apply to the new active document?&lt;BR /&gt;
&lt;BR /&gt;
IIRC, my ObjectAdded event will not fire until my command method has been executed from the command prompt. Why is that? It's almost as if "MyApp" is not created until the command method is executed.&lt;BR /&gt;
&lt;BR /&gt;
I want “MyApp” object to be a single instance. Should I declare it as: Public Shared Class Myapp?&lt;BR /&gt;
&lt;BR /&gt;
Overall, I want a single MyApp object and a single PaletteSet object to handle any active document the user throws at it. What should I be (not be) doing to make this so? (I should probably not be declaring a NEW PaletteSet in my command method.)</description>
      <pubDate>Wed, 03 Oct 2007 14:37:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082811#M76811</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T14:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082812#M76812</link>
      <description>In order for your event handler to be fired,&lt;BR /&gt;
there needs to be an instance of the class&lt;BR /&gt;
the handler is declared in. &lt;BR /&gt;
&lt;BR /&gt;
Have you wondered how an instance of your&lt;BR /&gt;
class is being created?&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JSPARK&gt; wrote in message news:5739156@discussion.autodesk.com...&lt;BR /&gt;
Thank you both for your time. It has been very enlightening. A few more questions with some sample code this time…&lt;BR /&gt;
&lt;BR /&gt;
In a nutshell, this is “MyApp” object:&lt;BR /&gt;
&lt;BR /&gt;
Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;
Imports Autodesk.AutoCAD.Interop&lt;BR /&gt;
&lt;BR /&gt;
Public Class MyApp&lt;BR /&gt;
&lt;BR /&gt;
‘Private Members&lt;BR /&gt;
Private WithEvents mThisAcad as AcadApplication = Ctype(ThisAcad.AcadApplication,  AcadApplication)&lt;BR /&gt;
Private WithEvents mThisDrawing as AcadDocument = Ctype(ThisDrawing.AcadDocument,  AcadDocument)&lt;BR /&gt;
&lt;BR /&gt;
‘Public Properties&lt;BR /&gt;
Public Shared ReadOnly Property ThisAcad as Application&lt;BR /&gt;
Get&lt;BR /&gt;
Return Application.AcadApplication&lt;BR /&gt;
End Get&lt;BR /&gt;
End Property&lt;BR /&gt;
&lt;BR /&gt;
Public Shared ReadOnly Property ThisDrawing as Document&lt;BR /&gt;
Get&lt;BR /&gt;
Return Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
End Get&lt;BR /&gt;
End Property&lt;BR /&gt;
&lt;BR /&gt;
‘Methods&lt;BR /&gt;
(Autodesk.AutoCAD.Runtime.CommandMethod("Start", "Start", _ Autodesk.AutoCAD.Runtime.CommandFlags.Modal)&amp;gt;  _&lt;BR /&gt;
Public Sub Start()&lt;BR /&gt;
‘Create a new PaletteSet and a Palette. Add Palette to PaletteSet.&lt;BR /&gt;
‘Set PaletteSet.Visible = True&lt;BR /&gt;
EndSub&lt;BR /&gt;
&lt;BR /&gt;
‘Events&lt;BR /&gt;
Private Sub mThisDrawing_ObjectAdded(ByVal Obj As Object) Handles _ mThisDrawing.ObjectAdded&lt;BR /&gt;
‘Do Stuff&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
End Class&lt;BR /&gt;
&lt;BR /&gt;
If I were to reset mThisDrawing = Ctype(ThisDrawing.AcadDocument, AcadDocument) each time a new Document becomes the active document, would my ObjectAdded event now only apply to the new active document?&lt;BR /&gt;
&lt;BR /&gt;
IIRC, my ObjectAdded event will not fire until my command method has been executed from the command prompt. Why is that? It's almost as if "MyApp" is not created until the command method is executed.&lt;BR /&gt;
&lt;BR /&gt;
I want “MyApp” object to be a single instance. Should I declare it as: Public Shared Class Myapp?&lt;BR /&gt;
&lt;BR /&gt;
Overall, I want a single MyApp object and a single PaletteSet object to handle any active document the user throws at it. What should I be (not be) doing to make this so? (I should probably not be declaring a NEW PaletteSet in my command method.)&lt;/JSPARK&gt;</description>
      <pubDate>Wed, 03 Oct 2007 15:44:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082812#M76812</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T15:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082813#M76813</link>
      <description>Yes, I have been wondering, and am unclear on when exactly "MyApp" is created. I would guess that NETLOAD does not create an instance of "MyApp". In order to execute the command method, an instance of "MyApp" is created... If thats the case then I would assume a second instance of "MyApp" is NOT created when the command is ran for a second time. So I dont need to worry about multiple instances of "MyApp" object being created. What about resetting mThisDrawing each time a new document becomes the active document? Will that make the same event handler work for the new active doc? (I guess I could do some tests to figure that out, but it sounds logical...) Am I on the right track here?</description>
      <pubDate>Wed, 03 Oct 2007 15:59:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082813#M76813</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T15:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082814#M76814</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
In the beginning I also struggled how to do it..look into the following:&lt;BR /&gt;
&lt;BR /&gt;
1. Best is to let the Assembly (dll) autoload into AutoCAD, this requires a registry key.&lt;BR /&gt;
&lt;BR /&gt;
2. Use a "Autodesk.AutoCAD.Runtime.IExtensionApplication" in you're app. As soon as the app gets loaded (NETLOAD or AutoLoad) then this class gets Initialized.&lt;BR /&gt;
&lt;BR /&gt;
On the "Initialize" you can create a Shared instance of the custom class that handles the documents. (including events)&lt;BR /&gt;
&lt;BR /&gt;
3. In you're Custom Class (let's name it: "acEvents_Manager" or so)&lt;BR /&gt;
In this class there have to be the Application Reference for handling events raised by the Application (ACAD), this depends on what events you will need.&lt;BR /&gt;
You also need a DocumentCollection Reference(instance)&lt;BR /&gt;
This one DocumentBecameCurrent, DocumentCreated etc....&lt;BR /&gt;
&lt;BR /&gt;
4. Form the events of the DocumentCollection you'll be able to react, depending on you're goals.&lt;BR /&gt;
&lt;BR /&gt;
5. You can then event "listen" to database events. If a object changes, you can eval it and possibly update information displayed on a Palette.&lt;BR /&gt;
&lt;BR /&gt;
Samples etc, of these can be found in this NG.&lt;BR /&gt;
Search for the following:&lt;BR /&gt;
"Autoload dll"&lt;BR /&gt;
"IExtensionApplication"&lt;BR /&gt;
"DocumentCollection"&lt;BR /&gt;
&lt;BR /&gt;
ps. You should also download the "ObjectARX SDK (2008)"&lt;BR /&gt;
There samples and documentation regarding .NET in it.&lt;BR /&gt;
It helped me a lot.&lt;BR /&gt;
(i You need it I can upload a sample in VB.net)&lt;BR /&gt;
&lt;BR /&gt;
I use the following config:&lt;BR /&gt;
AutoCAD 2008, VS2005, WinXP sp2&lt;BR /&gt;
&lt;BR /&gt;
Good luck,&lt;BR /&gt;
A. Caddie</description>
      <pubDate>Wed, 03 Oct 2007 16:37:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082814#M76814</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T16:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082815#M76815</link>
      <description>That is very helpful, thank you. I will search the NG using your suggestions. I did assume I would need to handle the DocumentCollection events in order to keep "mThisDrawing" up to date with the active document. Will my application class need to "Inherit" IExtensionApplication? I am not sure what you mean by  "Use  "Autodesk.AutoCAD.Runtime.IExtensionApplication" in you're app".  I will look into the IExtensionAppliciation. You also suggested that I create a new class for handling events. Is it "ok" to handel events within my main application class?</description>
      <pubDate>Wed, 03 Oct 2007 16:59:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082815#M76815</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T16:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082816#M76816</link>
      <description>What a nice professional response. You must have had some good action last&lt;BR /&gt;
night &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
Davis&lt;BR /&gt;
"Tony Tanzillo" &lt;TONY.TANZILLO&gt; wrote in message&lt;BR /&gt;
news:5739091@discussion.autodesk.com...&lt;BR /&gt;
I suppose that if you've been using VBA and its&lt;BR /&gt;
'ThisDrawing' object, you might become confused&lt;BR /&gt;
by what you take for granted.&lt;BR /&gt;
&lt;BR /&gt;
In VBA, 'ThisDrawing' looks like a variable, but is&lt;BR /&gt;
in reality, more like a function that always returns&lt;BR /&gt;
the Active document in the editor.&lt;BR /&gt;
&lt;BR /&gt;
So in .NET, rather than assigning a variable to&lt;BR /&gt;
the result of your GetDoc() function (which I&lt;BR /&gt;
presume returns the active document), you must&lt;BR /&gt;
call the GetDoc() function when want the Active&lt;BR /&gt;
document (or to make it easier, you can add a&lt;BR /&gt;
property to your class called 'ThisDrawing', and&lt;BR /&gt;
in the 'getter' for it, just return the active doc).&lt;BR /&gt;
&lt;BR /&gt;
When you're working with palettes, you should&lt;BR /&gt;
never cache a document object in a variable. You&lt;BR /&gt;
should always reference the MdiActiveDocument&lt;BR /&gt;
property of the DocumentCollection directly, when&lt;BR /&gt;
an operation begins, and then use that value only&lt;BR /&gt;
for the duration of the operation. An 'operation'&lt;BR /&gt;
means when the user clicks a button on your&lt;BR /&gt;
user interface, and in response, you access the&lt;BR /&gt;
active document to do something.&lt;BR /&gt;
&lt;BR /&gt;
What is also not obvious about VBA's 'ThisDrawing',&lt;BR /&gt;
is that when you handle an event of 'ThisDrawing',&lt;BR /&gt;
the event is fired for every open document, not&lt;BR /&gt;
just the one that was active when the event was&lt;BR /&gt;
assigned.&lt;BR /&gt;
&lt;BR /&gt;
In .NET, things are not as simple or automatic as&lt;BR /&gt;
they are in VBA.&lt;BR /&gt;
&lt;BR /&gt;
In .NET a Document object represents a single&lt;BR /&gt;
Document open in the editor, that has its own&lt;BR /&gt;
events. When you add a handler for a Document&lt;BR /&gt;
event to a Document, the handler only receives&lt;BR /&gt;
notification for the event in the Document that&lt;BR /&gt;
you added the handler to. So, to handle the same&lt;BR /&gt;
document event in every document, you must&lt;BR /&gt;
add the handler for the event to every document&lt;BR /&gt;
manually.&lt;BR /&gt;
&lt;BR /&gt;
In order to do that, you handle the events of the&lt;BR /&gt;
DocumentCollection (Application.DocumentManager),&lt;BR /&gt;
like DocumentAdded, and DocumentToBeDestroyed,&lt;BR /&gt;
and in the handlers for those events, you must add&lt;BR /&gt;
or remove your document event handlers to the&lt;BR /&gt;
newly added, or about-to-be closed document.&lt;BR /&gt;
&lt;BR /&gt;
If your app is not loaded at startup, then you also&lt;BR /&gt;
have to iterate over the collection of currently open&lt;BR /&gt;
documents, and add document events handlers to&lt;BR /&gt;
each of them.&lt;BR /&gt;
&lt;BR /&gt;
There's sample code showing how to do this on my&lt;BR /&gt;
website, but its in C#. There may be someone here&lt;BR /&gt;
that has converted it to VB.NET.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JSPARK&gt; wrote in message news:5738738@discussion.autodesk.com...&lt;BR /&gt;
I have a .NET app that is loaded with the NETLOAD command. I have a single&lt;BR /&gt;
command method that loads a custom control into a palette, and displays it.&lt;BR /&gt;
Whenever my code needs a reference to "ThisDrawing", "ThisAcad", "ThisDB"&lt;BR /&gt;
etc... it is gotten from a function that returns the needed reference. That&lt;BR /&gt;
said I have a few questions:&lt;BR /&gt;
&lt;BR /&gt;
Why do my event handlers (the app in general) only become active after I&lt;BR /&gt;
type the command for my command method? Why are they not functional after&lt;BR /&gt;
the NETLOAD command? Is my application object (the class that contains my&lt;BR /&gt;
command method and my withevents member declarations) only instantiated when&lt;BR /&gt;
a command method is called?&lt;BR /&gt;
&lt;BR /&gt;
Why does my app stop working when a new drawing is opened if the AcadApp,&lt;BR /&gt;
Document, Editor, Database etc.. references are gotten from a function?&lt;BR /&gt;
Example:&lt;BR /&gt;
&lt;BR /&gt;
Dim WithEvents ThisDrawing as Document = GetDoc()&lt;BR /&gt;
&lt;BR /&gt;
I think I am incorrectly assuming that each time my code uses ThisDrawing,&lt;BR /&gt;
it's the same as calling GetDoc(). Maybe I should explicitly set ThisDrawing&lt;BR /&gt;
= GetDoc() each time a new drawing becomes active?&lt;BR /&gt;
&lt;BR /&gt;
Any other tips on gracefully handling the transition from active document to&lt;BR /&gt;
active document will be much appreciated as well.&lt;BR /&gt;
&lt;BR /&gt;
TIA&lt;/JSPARK&gt;&lt;/TONY.TANZILLO&gt;</description>
      <pubDate>Wed, 03 Oct 2007 17:04:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082816#M76816</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T17:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082817#M76817</link>
      <description>It would be better to download the SDK and read everything concerning the entrypoint of an application. The method is simple enough and the examples are good.&lt;BR /&gt;
The entrypoint is just a method of one of your classes that is executed during load time. You just have to declare it in a certain way. It's simple enough after you see the examples.&lt;BR /&gt;
After you implement it, you can decide whether you want to store your variables in static variables and create a new instance of your class for every drawing or whatever.</description>
      <pubDate>Wed, 03 Oct 2007 17:49:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082817#M76817</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T17:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082818#M76818</link>
      <description>I have already downloaded the SDK but have not read much of the documentation yet. Thank you all so much. I believe I have enough homework to keep me busy for a while.</description>
      <pubDate>Wed, 03 Oct 2007 18:13:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082818#M76818</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T18:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082819#M76819</link>
      <description>That was almost too easy. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;</description>
      <pubDate>Wed, 03 Oct 2007 23:21:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082819#M76819</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T23:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082820#M76820</link>
      <description>You might find some of the info in this post useful.&lt;BR /&gt;
&lt;BR /&gt;
http://discussion.autodesk.com/thread.jspa?messageID=5538160</description>
      <pubDate>Wed, 03 Oct 2007 23:51:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082820#M76820</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-03T23:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082821#M76821</link>
      <description>"CADJunkie" wrote:&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt;&amp;gt; What a nice professional response. &lt;BR /&gt;
&lt;BR /&gt;
No, that's simply how all 'non-charletans'&lt;BR /&gt;
are treated.&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com</description>
      <pubDate>Thu, 04 Oct 2007 10:23:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082821#M76821</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-04T10:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle multiple open documents gracefully</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082822#M76822</link>
      <description>&amp;gt;&amp;gt; So I dont need to worry about multiple instances &lt;BR /&gt;
&amp;gt;&amp;gt; of "MyApp" object being created. &lt;BR /&gt;
&lt;BR /&gt;
Well, not quite.&lt;BR /&gt;
&lt;BR /&gt;
The way it works is that if your command method is&lt;BR /&gt;
non-static (not shared), as is the case in your code,&lt;BR /&gt;
AutoCAD creates a seperate instance of your class, &lt;BR /&gt;
for each open document. So, if there are 3 open &lt;BR /&gt;
documents, there will be three instances of your &lt;BR /&gt;
"MyApp" class.&lt;BR /&gt;
&lt;BR /&gt;
That may be what is leading you to think that VB is &lt;BR /&gt;
automatically switching the events every time you&lt;BR /&gt;
assign a non-static/shared member variable that has &lt;BR /&gt;
the 'WithEvents', to another document. &lt;BR /&gt;
&lt;BR /&gt;
In fact, what you don't realize is that there is more &lt;BR /&gt;
than one instance of your class, and each of them is &lt;BR /&gt;
being used with a different document, so it gives the &lt;BR /&gt;
illusion that the event handlers are automatically &lt;BR /&gt;
added to the document assigned to the variable each &lt;BR /&gt;
time the variable's value is changed, but that's not&lt;BR /&gt;
what's going on at all.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JSPARK&gt; wrote in message news:5739332@discussion.autodesk.com...&lt;BR /&gt;
Yes, I have been wondering, and am unclear on when exactly "MyApp" is created. I would guess that NETLOAD does not create an instance of "MyApp". In order to execute the command method, an instance of "MyApp" is created... If thats the case then I would assume a second instance of "MyApp" is NOT created when the command is ran for a second time. So I dont need to worry about multiple instances of "MyApp" object being created. What about resetting mThisDrawing each time a new document becomes the active document? Will that make the same event handler work for the new active doc? (I guess I could do some tests to figure that out, but it sounds logical...) Am I on the right track here?&lt;/JSPARK&gt;</description>
      <pubDate>Sat, 06 Oct 2007 01:26:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-handle-multiple-open-documents-gracefully/m-p/2082822#M76822</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-10-06T01:26:59Z</dc:date>
    </item>
  </channel>
</rss>

