Start listening to editor events when extension is enabled. vb.net addin

1824
3
Jump to solution
08-05-2016 02:04 PM
StacyMcNeil
New Contributor II

I have been struggling with getting two types of addins to work together for some time now.  I have created an editor extension addin and have been able to listen to events on start editing with success.  I also have created a regular (desktop) extension and can check it on and off with success.  What I am having trouble with is that I would like the editor event listener to only work when the extension in ArcMap is enabled (checked).  I am working with the desktop extension addin now but whenever I add the event handlers to start listening when the extension is enabled it causes ArcMap to crash.  Any ideas or sample code would be greatly appreciated!  The handlers are commented out since that is what is causing ArcMap to crash.

Imports ESRI.ArcGIS.Editor
Public Class MyEditListener
    Inherits ESRI.ArcGIS.Desktop.AddIns.Extension
    Public Sub New()
    End Sub
    Protected Overrides Sub OnStartup()
        ' TODO: Uncomment to start listening to document events
        'WireDocumentEvents()
    End Sub
    Private m_editorEvent As IEditEvents_Event
    Protected Overrides Function OnSetState(ByVal state As ESRI.ArcGIS.Desktop.AddIns.ExtensionState) As Boolean
        Me.State = state
        If state = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then
            MsgBox("Enabled")
            'AddHandler m_editorEvent.OnStartEditing, AddressOf Events_OnStartEditing
            'AddHandler m_editorEvent.OnStopEditing, AddressOf Events_OnStopEditing
        Else
            MsgBox("Not Enabled")
        End If
        Return MyBase.OnSetState(state)
    End Function
    Private Sub Events_OnStartEditing()
        MsgBox("start editing")
    End Sub
    Private Sub Events_OnStopEditing()
        MsgBox("stop editing")
    End Sub
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Stacy,

You have defined m_editorEvent, but not set it anywhere. You'll need to cast it from the editor.

View solution in original post

3 Replies
by Anonymous User
Not applicable

Stacy,

You have defined m_editorEvent, but not set it anywhere. You'll need to cast it from the editor.

KenBuja
MVP Esteemed Contributor

Here's how I do that. In the different editor events I have assigned, the first line in the function is

If Not (myAddin.Extension.IsExtensionEnabled) Then Exit Sub

The Extension class in my add-in contains these functions

Private Shared s_Extension As myAddin.Extension

Friend Shared Function IsExtensionEnabled() As Boolean
    If s_Extension Is Nothing Then
        GetExtension()    
    End If
    
    If s_Extension Is Nothing Then
        Return False
    End If

    Return s_Extension.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled

End Function

Private Shared Function GetExtension() As myAddin.Extension

    If s_Extension Is Nothing Then
        Dim extID As New ESRI.ArcGIS.esriSystem.UID
        extID.Value = My.ThisAddIn.IDs.Extension
        My.ArcMap.Application.FindExtensionByCLSID(extID)
    End If
    
    Return s_Extension

End Function
0 Kudos
StacyMcNeil
New Contributor II

Thank you for your answers!  Both have helped me solve different problems I had with my script.

0 Kudos