I suppose that if you've been using VBA and its
'ThisDrawing' object, you might become confused
by what you take for granted.
In VBA, 'ThisDrawing' looks like a variable, but is
in reality, more like a function that always returns
the Active document in the editor.
So in .NET, rather than assigning a variable to
the result of your GetDoc() function (which I
presume returns the active document), you must
call the GetDoc() function when want the Active
document (or to make it easier, you can add a
property to your class called 'ThisDrawing', and
in the 'getter' for it, just return the active doc).
When you're working with palettes, you should
never cache a document object in a variable. You
should always reference the MdiActiveDocument
property of the DocumentCollection directly, when
an operation begins, and then use that value only
for the duration of the operation. An 'operation'
means when the user clicks a button on your
user interface, and in response, you access the
active document to do something.
What is also not obvious about VBA's 'ThisDrawing',
is that when you handle an event of 'ThisDrawing',
the event is fired for every open document, not
just the one that was active when the event was
assigned.
In .NET, things are not as simple or automatic as
they are in VBA.
In .NET a Document object represents a single
Document open in the editor, that has its own
events. When you add a handler for a Document
event to a Document, the handler only receives
notification for the event in the Document that
you added the handler to. So, to handle the same
document event in every document, you must
add the handler for the event to every document
manually.
In order to do that, you handle the events of the
DocumentCollection (Application.DocumentManager),
like DocumentAdded, and DocumentToBeDestroyed,
and in the handlers for those events, you must add
or remove your document event handlers to the
newly added, or about-to-be closed document.
If your app is not loaded at startup, then you also
have to iterate over the collection of currently open
documents, and add document events handlers to
each of them.
There's sample code showing how to do this on my
website, but its in C#. There may be someone here
that has converted it to VB.NET.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
wrote in message news:5738738@discussion.autodesk.com...
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:
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?
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:
Dim WithEvents ThisDrawing as Document = GetDoc()
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?
Any other tips on gracefully handling the transition from active document to active document will be much appreciated as well.
TIA