SelectionChanged event not firing

6351
12
Jump to solution
06-01-2015 09:08 AM
DavidStefan
New Contributor III

I want to bind an action to the SelectionChanged event on the map. In the extension, I bind the action to the focused map's SelectionChanged event upon firing ArcMap.Events.NewDocument and ArcMap.Events.NewDocument. The assumption I'm making here is that upon firing of the New(Open)Document events, there is an active document with a map so I can indeed bind actions to its events. But it doesn't work.

What is the correct way of getting this to work?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;

namespace BaseAddin {

    public class BaseExt: ESRI.ArcGIS.Desktop.AddIns.Extension {

        public BaseExt() {
        }

        protected override void OnStartup() {
            WireUpEvents();
        }

        private void WireUpEvents() {
            ArcMap.Events.NewDocument += ArcMap_NewOpenDocument;
            ArcMap.Events.OpenDocument += ArcMap_NewOpenDocument;
        }

        private void ArcMap_NewOpenDocument() {

            IMxDocument doc = ArcMap.Document as IMxDocument;

            ((IActiveViewEvents_Event)doc.FocusMap).SelectionChanged +=
                new IActiveViewEvents_SelectionChangedEventHandler(delegate() {
                    System.Windows.Forms.MessageBox.Show("Feature selection changed.");
            });
        }
    }
}
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidStefan
New Contributor III

After much trial and error, I have figured out how to solve this. My extension is configured to load automatically upon ArcMap startup and I must bind my action to the SelectionChanged on Document.ActiveView only after the ArcMap.Events.OpenDocument or ArcMap.Events.NewDocument have been fired. This will ensure that the document is ready to accept event bindings. See code below for an illustration.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;

namespace BaseAddin {

    public class BaseExt: ESRI.ArcGIS.Desktop.AddIns.Extension {

        private static IActiveViewEvents_Event ViewEvents {
            get { return ArcMap.Document.ActiveView as IActiveViewEvents_Event; }
        }

        public BaseExt() {
        }


        protected override void OnStartup() {

            ArcMap.Events.OpenDocument += delegate() {
                ViewEvents.SelectionChanged += SelectionChanged_Handler;
            };

            ArcMap.Events.NewDocument += delegate() {
                ViewEvents.SelectionChanged += SelectionChanged_Handler;
            };
        }

        private void SelectionChanged_Handler() {
            MessageBox.Show("SelectionChanged");
        }
    }
}

View solution in original post

0 Kudos
12 Replies
AlexanderNohe1
Occasional Contributor III

Hi David,

It looks above that you are trying to fire the method.  I was able to get this to work by doing the following:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;


namespace selChanged
{
    public class Extension1 : ESRI.ArcGIS.Desktop.AddIns.Extension
    {


        private static IActiveViewEvents_Event Events { get { return ArcMap.Document.ActiveView as IActiveViewEvents_Event; } }


        public Extension1()
        {
        }


        protected override void OnStartup()
        {
            Events.SelectionChanged += Events_SelectionChanged;


        }


        void Events_SelectionChanged()
        {
            MessageBox.Show("Selection Changed");
        }




    }


}

Let me know if this works for you

Thanks

0 Kudos
DavidStefan
New Contributor III

Sadly, this code crashes my ArcMap. In particular, line 28:

Events.SelectionChanged += Events_SelectionChanged;

doesn't allow ArcMap to start and instead displays a dialogue with "ArcGIS Desktop has encountered a serious application error and is unable to continue."

Changing the code to

((IActiveViewEvents_Event)ArcMap.Document.ActiveView).SelectionChanged += delegate() {
                MessageBox.Show("Selection Changed");
            };

results in the same error.

I am running ArcGIS Desktop 10.1 and matching Arcobjects .NET SDK.

0 Kudos
AlexanderNohe1
Occasional Contributor III

What I wrote works for me at 10.3.  I might be able to test on 10.1 a little later today.  Do you have any service packs installed or any other components installed?

0 Kudos
DavidStefan
New Contributor III

No, it's should be a clean 10.1 install. I've also deleted all other add-ins so the only extension added is the one I'm testing.

Thanks for your help, by the way. I've been pulling my hair over this for two days!

0 Kudos
AlexanderNohe1
Occasional Contributor III

I can't reproduce with a clean 10.1 install using the same code provided to you earlier.

0 Kudos
DavidStefan
New Contributor III

I've just changed the extension setting from autoLoad="true" to autoLoad="false". Now when I open ArcMap, then go to Customize -> Extensions and select "Base Extension" it works as expected.

However, when I close and re-open the app, the behaviour falls back to not firing the SelectionChanged event (or, as I imagine, not binding the event to the desired action).

0 Kudos
AlexanderNohe1
Occasional Contributor III

Is the extension still checked on in the extensions menu when you reload the app?

0 Kudos
DavidStefan
New Contributor III

Yes it is. And getting to this point has helped me solve this! I finally figured I have to listen to ArcMap.Events.OpenDocument and bind the SelectionChanged only after the former has been fired. Thanks for you help, my final answer shows the working code.

0 Kudos
AlexanderNohe1
Occasional Contributor III

Great!  I am glad you got it working!

0 Kudos