Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I can't seem to wire up IActiveViewEvents for an IDockableWindow. I am using a straight out-of-the-box IDockableWindow which inherits BaseCommand. The snippet below shows what I have added to the autogenerated code. Problem is, when I try to run ArcMap, it (the main ArcMap GUI) never loads. Please see attached screenshot to see what I mean.bland arcmap gui

 public override void OnCreate(object hook)
    {
        if (hook != null)
            m_application = hook as IApplication;

        if (m_application != null)
        {
            SetupDockableWindow();
            base.m_enabled = m_dockableWindow != null;
        }
        else
            base.m_enabled = false;

        IMxDocument mxDoc = (IMxDocument)m_application.Document;
        IActiveViewEvents_Event activeViewEvents = mxDoc.ActiveView as IActiveViewEvents_Event;
        activeViewEvents.ViewRefreshed += new IActiveViewEvents_ViewRefreshedEventHandler(activeViewEvents_ViewRefreshed);
    }

    void activeViewEvents_ViewRefreshed(IActiveView view, esriViewDrawPhase phase, object Data, ESRI.ArcGIS.Geometry.IEnvelope envelope)
    {
        MessageBox.Show("ViewRefreshed");
    }
share|improve this question
    
Is the event being triggered lots and lots of times? You probably should try to hook the event after the OnCreate method, on the first time the Dockable Window is shown. –  George Feb 24 '12 at 13:26
add comment

2 Answers

I'm not sure if this is the source of your problem, but I would suggest not applying the events to the mxDoc.ActiveView within the OnCreate event of the dockable window. The active view can change for a variety of reasons, (user adds a new data frame or opens a new map document). By assigning the events in the OnCreate event you are locking the event handler to the current active view at that point, which means only that view will respond to the event. This creates problems for a number of reasons. For example, if you open ArcMap directly, the currently active view will be the active view in the default mdx. If the user then selects a map document to open, that event handler no longer applies because the active view you applied it to no longer exists.

I typically assign active view events by first creating an ArcMap extension, and then creating an event handler for OnActiveViewChanged. This event is setup either in the Startup method for a blank extension or in the Set method of the State property in a configurable extension. Next I create a private method in the extension for SetupActiveViewEvents and RemoveActiveViewEvents. Whenever the active view is changed I cycle through all of the maps in the current mxDoc and run RemoveActiveViewEvents, and then run SetupActiveViewEvents on the current ActiveView. This does two things, first it insures that my events are visible to all data frames, and second insures the events are not applied multiple times to the same view.

Others may have a better approach, but this has worked very well for me. The example below sets up selection change events on active views. In your case, rather than wiring OnActiveViewSelectionChanged, you could wire the view refresh event.

   '''In the extension, setup the OnActiveViewEvent
    Private Sub SetupDocumentEvents(ByRef mxDoc As IMxDocument)
        m_docEvents = CType(mxDoc, IDocumentEvents_Event)
        AddHandler m_docEvents.ActiveViewChanged, AddressOf OnActiveViewChanged
    End Sub

    ''' ActiveView Changed Event handler
    Private Sub OnActiveViewChanged()
        Dim maps As IMaps2
        Dim map As IMap
        maps = m_mxDoc.Maps

        For i As Integer = 0 To maps.Count - 1
            map = maps.Item(i)
            Me.RemoveActiveViewEvents(map)
        Next

        m_activeView = m_mxDoc.ActiveView
        m_focusMap = m_activeView.FocusMap
        Me.SetupActiveViewEvents(m_focusMap)
    End Sub

    ''' Set up the event handlers for the IActiveViewEvents
    Private Sub SetupActiveViewEvents(ByRef map As ESRI.ArcGIS.Carto.IMap)
        m_activeViewEvents = CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event)
        m_ActiveViewEventsSelectionChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(AddressOf OnActiveViewEventsSelectionChanged)
        AddHandler m_activeViewEvents.SelectionChanged, m_ActiveViewEventsSelectionChanged
    End Sub

    ''' Remove the event handlers for the IActiveViewEvents
    Private Sub RemoveActiveViewEvents(ByRef map As ESRI.ArcGIS.Carto.IMap)
        Dim activeViewEvents As ESRI.ArcGIS.Carto.IActiveViewEvents_Event = CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event)
        RemoveHandler activeViewEvents.SelectionChanged, m_ActiveViewEventsSelectionChanged
    End Sub
share|improve this answer
add comment

I have a similar issue. I have to display some messages when the user selects some features using the selection tool. I have an arcmap extension which has the on activeview changed event set up and adds and removes Selection Changed event handler. The document event fires, but not the selection changed. I am using 9.3 and VS 2008. I have pasted the code here.

I am out of my wits and any help will be really great, Thanks in advance.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF.CATIDs; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.ArcMapUI;

namespace TestExtension { [Guid("1BA7C200-B664-4358-88C9-8D2DD82D3416")] [ClassInterface(ClassInterfaceType.None)] [ProgId("TestExtension.ArcMapExtension")] public class ArcMapExtension : IExtension { private ESRI.ArcGIS.Framework.IApplication m_application; private IMxDocument m_MxDocument; private IActiveView m_ActiveView; private IMap m_Map; IDocumentEvents_Event documentEvent;

    private IActiveViewEvents_Event m_activeviewselectionchangedEvents;

    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
        // Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType);

        //
        // TODO: Add any COM registration code here
        //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
        // Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType);

        //
        // TODO: Add any COM unregistration code here
        //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
        string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
        MxExtension.Register(regKey);
        GMxExtensions.Register(regKey);
        SxExtensions.Register(regKey);

    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
        string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
        MxExtension.Unregister(regKey);
        GMxExtensions.Unregister(regKey);
        SxExtensions.Unregister(regKey);

    }

    #endregion
    #endregion

    #region "IExtension Implementations"
    public string Name
    {
        get
        {
            // TODO: Add ArcMapExtension.Name getter implementation

            return "Monitor Selection";
        }
    }

    public void Shutdown()
    {
        // TODO: Add ArcMapExtension.Shutdown implementation
    }

    public void Startup(ref object initializationData)
    {
        m_application = initializationData as IApplication;


        m_MxDocument =(IMxDocument) m_application.Document;

       m_ActiveView = m_MxDocument.ActiveView;
       m_Map = m_ActiveView.FocusMap;

       this.SetUpDocumentEvents();






    }
    #endregion


    private void SetUpDocumentEvents()
    {
        documentEvent = (IDocumentEvents_Event)m_application.Document;
        documentEvent.ActiveViewChanged += new IDocumentEvents_ActiveViewChangedEventHandler(this.OnActiveViewChanged);
    }

    private void SetUpActiveViewEvents(IMap map)
    {
        m_activeviewselectionchangedEvents = (IActiveViewEvents_Event)m_Map;
        m_activeviewselectionchangedEvents.SelectionChanged += new IActiveViewEvents_SelectionChangedEventHandler(this.CommunicatetoExternalApp);


    }

    private void RemoveActiveViewEvents(IMap map)
    {
        m_activeviewselectionchangedEvents = (IActiveViewEvents_Event)m_Map;



        m_activeviewselectionchangedEvents.SelectionChanged -= new IActiveViewEvents_SelectionChangedEventHandler(this.CommunicatetoExternalApp);

    }


    public void OnActiveViewChanged()
    {

        IMaps2 maps;
        m_MxDocument = (IMxDocument)m_application.Document;
        maps =(IMaps2) m_MxDocument.Maps;
        for (int i = 0; i < maps.Count; i++)
        {
            m_Map = maps.get_Item(i);
            this.RemoveActiveViewEvents(m_Map);


        }

        m_ActiveView = m_MxDocument.ActiveView;
        m_Map = m_ActiveView.FocusMap;
        this.SetUpActiveViewEvents(m_Map);



    }


    public void CommunicatetoExternalApp()
    {
      //  MessageBox.Show("Hurray");

        Console.WriteLine("Hurray");
        //send message to Gen 2 

    }


/*    #region "IExtensionConfig Implementations"
    public string Description
    {
        get
        {
            // TODO: Add ArcMapExtension.Description getter implementation
            return default(string);
        }
    }

    public string ProductName
    {
        get
        {
            // TODO: Add ArcMapExtension.ProductName getter implementation
            return default(string);
        }
    }

    public ESRI.ArcGIS.esriSystem.esriExtensionState State
    {
        get
        {
            // TODO: Add ArcMapExtension.State getter implementation
            return default(ESRI.ArcGIS.esriSystem.esriExtensionState);
        }
        set
        {
            // TODO: Add ArcMapExtension.State setter implementation
        }
    }
    #endregion*/


}

}

share|improve this answer
1  
This probably should be a new question. –  Kirk Kuykendall Mar 6 '12 at 22:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.