SelectionChanged event not firing

6361
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
12 Replies
DavidStefan
New Contributor III

Update: Changing target framework form 4.0 to 3.5 resolves the error described above, but I still do NOT get the message to show upon selecting features...

0 Kudos
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");
        }
    }
}
0 Kudos
ChrisBrannin
Occasional Contributor

Any reason this wouldn't work with ItemAdded/ItemRemoved?

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

void Event_OnOpenDoc()
        {
            ArcMap.Events.OpenDocument += delegate() { Events.ItemAdded += Events_ItemAdded; };
        }

private void Events_ItemAdded(Object item)
        {
            MessageBox.Show("ItemAdded");
        } 

I am able to run this without error but nothing happens when an item is added to the view. I feel like I'm missing something or using this incorrectly. Any help with this is greatly appreciated!

0 Kudos