.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

the "DocumentBecameCurrent" event

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
810 Views, 2 Replies

the "DocumentBecameCurrent" event

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?
Thanks, Perry
2 REPLIES 2
Message 2 of 3
e.vallauri
in reply to: Anonymous

Hi perry,

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
Else
flag_docBecameCurrent_done = False
End If
End If
End Sub

IsActive readonly property helps to show only the document that became current, and the flag make your code run only once.

I think that's sufficient to solve the problem.

- David
Message 3 of 3
Sameeksha
in reply to: e.vallauri

Thanks a lot for your reply, it was very helpful to me!!

 

I ended up putting this kind of code in my app to fire only when the event refers to an active document that is different from the one stored in-memory from last time.

 

// DocumentBecameCurrent event fires multiple times for a document switch, it fires first with the new document with isActive = true,

// then fires with the old document with isActive false and then once again with the new document with isActive = true.

// Hence we put this double check below of processing only if active and different from the document stored in memory.(currentDocument)

// The world outside this function will only see one event DocumentChanged (custom event) bubbled correctly.

var isActive = e.Document.IsActive;

if (isActive)

{

var documentThatJustBecameCurrent = e.Document == null ? null : e.Document.Name;

if (!String.Equals(documentThatJustBecameCurrent, currentDocument))

 

{

currentDocument = documentThatJustBecameCurrent;

DocumentChanged(sender, e);

}

}

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost