In an app I'm working on I'm using the DocumentBecameCurrent event so
that I can update some global data, dialogs & stuff when a user switches
between open documents. This seems to work just fine except in one case.
When Acad first starts it starts with a new, empty drawings (yes, I know
I can change this behavior). If I open an existing drawing without first
closing the "startup" drawing it seems the DocumentBecameCurrent event
fires twice. It appears to fire then the existing drawing is opened, then
in the background Acad switches focus to the startup drawing to close it,
effectively losing focus on the existing drawing, then puts its focus
back on the exising drawing. This causes the the DocumentBecameCurrent
event to fire twice on the same drawing.
Its a bit annoying and has side effects on my global vars.
The easy answer would be to close any "startup" drawing before opening a
new one, but I cant count on that.
Can anyone suggest a way to avoid this double firing?
I know that the answer comes a few months later, but I hope this should help somebody later...
An easy solution, that is often used is to add a flag.
Private Shared flag_docBecameCurrent_done As Boolean = False
Private Sub docBecameCurrent(ByVal o As Object, ByVal e As DocumentCollectionEventArgs)
If (e.Document.IsActive) Then
If Not (flag_docBecameCurrent_done) Then
' YOUR CODE
flag_docBecameCurrent_done = True
flag_docBecameCurrent_done = False
IsActive readonly property helps to show only the document that became current, and the flag make your code run only once.