This project is read-only.

get a handle on the map layer by name or some uniqe value

Sep 11, 2014 at 9:36 AM
Hi,I want to get handel of a spatial map layer in my project.I can not use IMapFeatureLayer lay = (IMapFeatureLayer)map1.Layers[0]
because it changes when I add new layer to map,How can I handel a map layer with a uniqe name or sth like that?
Is there any way to find an spatial map layer even if user changes its name in legend or add a new layer to map?
Sep 11, 2014 at 11:07 AM
Hey am2,

just search through all layers in map1.Layers and look whether the DataSet.Name equals the name of the layer your searching. Here an examplary function, that you can feed with map1.Layers to look for the layer your searching.
        /// <summary>
        /// Find a layer with a certain name.
        /// </summary>
        /// <param name="layers">Collection with layers to search through.</param>
        /// <param name="pLayer">Variable to catch the found layer.</param>
        /// <param name="LayerName">Name of the layer we're looking for.</param>
        /// <returns>True, if the layer was found.</returns>
        private bool FindLayer(IMapLayerCollection layers, ref ILayer pLayer, string LayerName)
            for (int i = 0; i < layers.Count; i++)
                pLayer = layers[i];
                if ((pLayer) is MapGroup)
                    if (FindLayer(((MapGroup)pLayer).Layers, ref pLayer, LayerName)) return true;
                else if (pLayer.DataSet != null && !string.IsNullOrWhiteSpace(pLayer.DataSet.Name))
                    if (pLayer.DataSet.Name == LayerName) return true;
            return false;