1

What I want to achieve is to remove specific layers from the TOC either based on their array place [n] or their name (which would be ideal in case their place in the TOC changed).

The only thing I can find is the ClearLayers() method to clear all of the layers from the TOC.

Also I am aiming to remove a featurelayer and a table.

closed as off-topic by PolyGeo Aug 7 '17 at 20:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

2

I use this code and it works.........

private void OUT(string line)
{
            IMap Map;
            IMxDocument mxDocument;
            IApplication App = ArcMap.Application;
            mxDocument = (IMxDocument)(App.Document);
            Map = mxDocument.FocusMap;

            var pDelLayerLine = (IFeatureLayer)Map.get_Layer(0);

            IWorkspaceFactory pWorkSpaceFactory = new FileGDBWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace (IFeatureWorkspace)pWorkSpaceFactory.OpenFromFile
                (@"C:\HER YOU MUST TO DEFINE THE PATH OF YOUR FEATURE......gdb", 0);

            int x = Map.LayerCount;

            for (int i = 0; (i < x); i++)
            {
                if (Map.get_Layer(i).Name == linea)
                {
                    pDelLayerLine = (IFeatureLayer)Map.get_Layer(i);
                }
            }
            mxDocument.FocusMap.DeleteLayer(pDelLayerLine);
            mxDocument.UpdateContents();
            mxDocument.ActiveView.Refresh();
}

I hope it will helps.....

  • This isn't working and I think the reason is because 1. a table isn't a feature class and 2. I'm trying to remove an xy event layer which wouldn't be in the gdb, it is only in memory – gisgirl Mar 23 '15 at 21:25
3

There is a better way to iterate through layers, look at Map.Layers(UID,True) as IEnumLayers... this limits your layer types (IFeatureLayer, IRasterLayer..) and looks in your group layers.

UID pUID = new UIDClass();
pUID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; // IFeatureLayers

IEnumLayer pMapLayers = ArcMap.Document.FocusMap.get_Layers(pUID, true);
ILayer pThisLayer = pMapLayers.Next();
while (pThisLayer != null)
{
    // not really necessary but I'll include this anyway
    if (pThisLayer.Valid)
    {
            if (pThisLayer.Name == "Layer name that I'm expecting")
            {
                ArcMap.Document.FocusMap.DeleteLayer(pThisLayer); 
            }
            // but what about from a particular database?
            IFeatureLayer pFtLayer = (IFeatureLayer)pThisLayer;
            IWorkspace pWS = (pFtLayer.FeatureClass as IDataset).Workspace;
            if (pWS.PathName == "Remove from this path")
            {
                ArcMap.Document.FocusMap.DeleteLayer(pThisLayer);
            }
    }
    else
    {
        // datasource not valid, remove anyway
        ArcMap.Document.FocusMap.DeleteLayer(pThisLayer); 
    }
    pThisLayer = pMapLayers.Next();
}
ArcMap.Document.UpdateContents(); //update the TOC
ArcMap.Document.ActivatedView.Refresh(); // refresh the view

There's a few techniques for working with layers that should help you in your coding. By supplying the UID you ensure that only IFeatureLayers are enumerated, ILayer.valid will tell you if the datasource is valid (or not, with red exclamation mark) and a little simple way of finding the workspace from a layer...

Note: you can delete layers in this enumeration and still continue as the indices are still valid. If you use for (I = 0;I < Map.LayerCount;I++) then when you remove a layer you straight away break the index and skip the next layer. If you must use simple iteration then you have to use for (I=Map.LayerCount;I>-1;I--) to iterate, reverse order doesn't break the index.

2

With a little modification, I got this to work.

This is important to note that this is for feature layers only, not tables.

        IMap map;
        IMxDocument mxDocument;
        mxDocument = (IMxDocument)ArcMap.Application.Document;
        map = mxDocument.FocusMap;
        mxDocument.ActiveView.Refresh();

        IFeatureLayer featureLayer;
        featureLayer = (IFeatureLayer)map.Layer[0]; // 1) Get a FeatureLayer

        IWorkspaceFactory workspaceFactory;
        workspaceFactory = new FileGDBWorkspaceFactory();


        int x = map.LayerCount;
        for (int i = 0; (i < x); i++)
        {
            if (map.get_Layer(i).Name == name)
            {
                featureLayer = (IFeatureLayer)map.get_Layer(i);
            }
        }

        mxDocument.FocusMap.DeleteLayer(featureLayer);
        mxDocument.UpdateContents();
        mxDocument.ActiveView.Refresh();
    }
  • You can improve this code further, you create a workspacefactory but never use it, delete that code. – Hornbydd Mar 23 '15 at 21:48
  • Thank you, very much appreciated. Do you have any idea how to remove a table by any chance? – gisgirl Mar 23 '15 at 21:51
1

To remove a table from the TOC you would use the interface IStandAloneTableCollection.

So in VB code would go something like:

dim pSTC as IStandAloneTableCollection
pSTC = pMap
dim pST as IStandaloneTable
pST =pSTC.StandaloneTable(x) ' x is index number
pSTC.RemoveStandaloneTable(pST)

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