2 Replies Latest reply on Apr 15, 2010 8:40 PM by priyanka_mehta

    Remove layer from TOC depending on its path

    priyanka_mehta
      Hi all,
         I have like 2 layers in my TOC. both have exactly the SAME name. but both are in different locations.

      like : "C:\temp\Original.mdb\FD\RoadFeature"

      and another is "C:\Updated.mdb\FD\RoadFeature"

      Is there a way to remove only the "RoadFeature" feature class that is located in Original.mdb from TOC

      Thanks,
      Priyanka Mehta
        • Removing the layer from TOC by comparing the path of the layer
          Alagappan
          Hi,

          Hope the below code solves your problem, which loops through the layers in TOC, compares the path of each layer to the required layer path that is to be removed and then remove that layer from TOC.

          Public Overrides Sub OnClick()
                  Try
                      Dim pMxdoc As IMxDocument = m_application.Document
                      Dim pMap As IMap = pMxdoc.FocusMap
                      Dim pEnumLayer As IEnumLayer = pMap.Layers
                      Dim pLayer As ILayer = pEnumLayer.Next
                      While Not player Is Nothing
                          Dim pFeatClass As IFeatureClass = CType(pLayer, IFeatureLayer).FeatureClass
                          Dim pDataset As IDataset = CType(pFeatClass, IDataset)
                          'obtaining the path of the layer in TOC
                          Dim strPath As String = pDataset.Workspace.PathName
                          'comparing the path of the layer with the required path to be removed
                          If strPath = "C:\temp\Original.mdb" Then
                              'if yes, remove the layer from TOC
                              pMap.DeleteLayer(pLayer)
                              Exit While
                          End If
                          pLayer = pEnumLayer.Next
                      End While
          End Sub


          Regards,
          Alagappan
          • Remove layer from TOC depending on its path
            priyanka_mehta
            Hey thanks a lot !.. It works well

            Thanks and Regards,
            Priyanka