A question on nested events

A question on nested events

Devin_11
Contributor Contributor
436 Views
3 Replies
Message 1 of 4

A question on nested events

Devin_11
Contributor
Contributor

I am writing a code wherein I am utilizing two events:

1. A "Document Created" event, which is registered when a  new document is created.

2. Inside the event handler of the "Document Created" event I have initialized a "Implied Selection Changed " event so that as soon as a new document is created, the Implied Selection Changed event is invoked.

But as I create multiple new documents and switch back to the old ones, the "Implied Selection Changed " event doesn't seem to work.

I do not understand why this is happening. 

Basically, I want "ImpliedSelectionChanged" event to work on every document that is created/open , looking for advice on how to go about this.

 

Thanks in advance.

0 Likes
437 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

You should show some code. Specifically how do you register the events (Commands or IExtensionApplication).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

Devin_11
Contributor
Contributor

Thank you for the response. Yes you are right, DocumentActivated, will work  but the problem with DocumentActivated is, the ImpliedSelectionChanged event will be registered every time the document is activated even if it was registered previously (assume an example of switching between documents). 

Also the problem here is, if an event is registered when the document is created which is the ImpliedSelectionChanged event in this case, is there any reason the event is not being triggered(after working for maybe 4-5 times) when switching between documents?  I am assuming that once an event is registered it must work until it is deregistered?

 

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

This sample works for me

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using System;

[assembly: ExtensionApplication(typeof(EventHandlingSample.Initialization))]

namespace EventHandlingSample
{
    public class Initialization : IExtensionApplication
    {
        public void Initialize()
        {
            Application.Idle += OnIdle;
        }

        private void OnIdle(object sender, EventArgs e)
        {
            var docs = Application.DocumentManager;
            var doc = docs.MdiActiveDocument;
            if (doc != null)
            {
                Application.Idle -= OnIdle;
                doc.Editor.WriteMessage("\nEventHandlingSample loaded.\n");
                doc.ImpliedSelectionChanged += Doc_ImpliedSelectionChanged;
                docs.DocumentCreated += Docs_DocumentCreated;
            }
        }

        private void Docs_DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            e.Document.ImpliedSelectionChanged += Doc_ImpliedSelectionChanged;
        }

        private void Doc_ImpliedSelectionChanged(object sender, EventArgs e)
        {
            Application.ShowAlertDialog("Implied Selection Changed");
        }

        public void Terminate()
        { }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub