Customer Service | Training | Contact Us
You are here: Home > User Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects 3rd Party IDE (C++, VB, C#, Delphi, etc.) forum

Custom Remove Layers button not firing Acti...   stefan coe Mar 01, 2007
Re: Custom Remove Layers button not firing...   Miles Hitchen Mar 05, 2007
Re: Custom Remove Layers button not firing...   stefan coe Mar 05, 2007
Re: Custom Remove Layers button not firing...   Miles Hitchen Mar 05, 2007
Re: Custom Remove Layers button not firing...   Kirk Kuykendall Mar 05, 2007
Report Inappropriate Content • Top • Print • Reply    
Subject Custom Remove Layers button not firing ActiveViewEventsItemDeleted 
Author stefan coe 
Date Mar 01, 2007 
Message I have a custom button that removes all the layers in the TOC when clicked. I would think this would fire the ActiveViewEventsItemDeleted event but it does not. I know I have the event wired properly because if I right click on a layer in the TOC and delete it, the ActiveViewEventsItemDeleted fires (I have it open a messagebox on the OnActiveViewEventsItemDeleted Sub). I also have a button that launches a form where layers can be added to the TOC and this triggers the ActiveViewEventsItemAdded event every time (when a layer is indeed added). Seems like if one works so should the other. Any ideas? Thanks in advance. 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Custom Remove Layers button not firing ActiveViewEventsItemDeleted 
Author Miles Hitchen 
Date Mar 05, 2007 
Message Hi Stefan,

I've just written a listener for the Map and a sub that deletes all the layers in the map and it fires the ItemDeleted event every time. Can you post your code so I cann have a look.

My version of the code is below.

Cheers,
Miles.
 
 
' ============= ThisDocument Code ================
' Must run StartListener() before deleting any layers
Private WithEvents m_pActiveViewEvents As Map

Public Sub StartListener()
Dim pMxDoc As IMxDocument

    Set pMxDoc = ThisDocument
    Set m_pActiveViewEvents = pMxDoc.FocusMap

End Sub


Public Sub StopListener()

    Set m_pActiveViewEvents = Nothing

End Sub

Private Sub m_pActiveViewEvents_ItemDeleted(ByVal Item As Variant)
    MsgBox "Suminks been deleted"
End Sub


' ============= Module1 Code ==================
Public Sub DeleteAllLayers()
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pLyr As ILayer

    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    While pMap.LayerCount > 0
        Set pLyr = pMap.Layer(0)
        pMap.DeleteLayer pLyr
    Wend

End Sub
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Custom Remove Layers button not firing ActiveViewEventsItemDeleted 
Author stefan coe 
Date Mar 05, 2007 
Message Sure, here is. It’s written in VB.Net. This class references at least one function in another module but it should be pretty obvious what is going on code wise. Let me know if you want it as an attachment. Thanks! 
 
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports System.Windows.Forms


<ComClass(RemoveAllLayersCmd.ClassId, RemoveAllLayersCmd.InterfaceId, RemoveAllLayersCmd.EventsId), _
 ProgId("PSRCTools.RemoveAllLayersCmd")> _
Public NotInheritable Class RemoveAllLayersCmd
    Inherits BaseCommand

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "5cc3bcf2-27da-459c-9567-2038b9849a3d"
    Public Const InterfaceId As String = "c8b6c42d-26b1-4227-bb82-65a9bc625148"
    Public Const EventsId As String = "8d2400e1-67f3-4699-8b47-3df98903522b"
#End Region

#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)

        'Add any COM registration code after the ArcGISCategoryRegistration() call

    End Sub

    <ComUnregisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)

        'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

    End Sub

#Region "ArcGIS Component Category Registrar generated code"
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Register(regKey)

    End Sub
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Unregister(regKey)

    End Sub

#End Region
#End Region

    Private m_ActiveViewEventsItemDeleted As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler
    Private m_application As IApplication
    Private pMxdoc As IMxDocument
    Private pAView As ESRI.ArcGIS.Carto.IActiveView
    Private pMap As ESRI.ArcGIS.Carto.IMap


    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()

        ' TODO: Define values for the public properties
        MyBase.m_category = ""  'localizable text 

        MyBase.m_caption = "Remove All Layers"   'localizable text 

        MyBase.m_message = "Removes All Layers"   'localizable text 

        MyBase.m_toolTip = "Click to Remove All Layers" 'localizable text 

        MyBase.m_name = "Remove All Layers"  'unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")

        Try
            'TODO: change bitmap name if necessary
            m_bitmap = New Bitmap(Me.GetType.Assembly.GetManifestResourceStream("PSRCTools.layers_check_off.bmp"))
            'Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
            'MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)

        Catch ex As Exception

            System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")

        End Try


    End Sub


    Public Overrides Sub OnCreate(ByVal hook As Object)

        If Not hook Is Nothing Then

            m_application = CType(hook, IApplication)

            'Disable if it is not ArcMap

            If TypeOf hook Is IMxApplication Then

                MyBase.m_enabled = True
            Else

                MyBase.m_enabled = False

            End If

        End If


        pMxdoc = m_application.Document

        pMap = pMxdoc.FocusMap

        pAView = pMap
        WireEvents()


    End Sub

    Public Overrides Sub OnClick()


        'If MessageBox.Show("Are you sure you want to remove all layers from the Table of Contents?", "Remove all layers?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then

        
        'Dim LayersOn As Boolean = False

       
        If CheckForLayers(pMap) = False Then
            MessageBox.Show("There are no Layers to Remove", "No Layers in the TOC", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If MessageBox.Show("Are you sure you want to remove all layers from the Table of Contents?", "Remove all layers?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then


            pMap.ClearLayers()

            pMxdoc.UpdateContents()

            pAView.Refresh()

        Else

            Exit Sub

        End If

    End Sub
    Public Sub WireEvents()
        m_ActiveViewEventsItemDeleted = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler(AddressOf OnActiveViewEventsItemDeleted)
        AddHandler CType(pMap, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemDeleted, m_ActiveViewEventsItemDeleted

    End Sub
    
    Private Sub OnActiveViewEventsItemDeleted(ByVal Item As Object)
        MessageBox.Show("Item Deleted")
    

    End Sub

End Class
 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Custom Remove Layers button not firing ActiveViewEventsItemDeleted 
Author Miles Hitchen 
Date Mar 05, 2007 
Message Hi Stefan,

The problem is that the method pMap.ClearLayers() doesn't fire any event. If you want the map events to fire then you'll need to use something similar to the code I posted that delete each layer individually.

Cheers,
Miles.
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: Custom Remove Layers button not firing ActiveViewEventsItemDeleted 
Author Kirk Kuykendall 
Date Mar 05, 2007 
Message Also should not put the listener in an ICommand, but in an extension. The extension will also need to listen for focus map changed, and rewire accordingly. This is a pain, a good reason I think ESRI should have something like an IMapExtensions interface on the Map coclass analogous to the ILayerExtensions for layers. This would be a logical place to put a listener for layers being removed.
 
  Kirk Kuykendall
AmberGIS Programming Services & Sales
web: http://www.ambergis.com
blog: http://ambergis.wordpress.com/
LinkedIn: http://www.linkedin.com/in/kirkkuykendall