Moving layer order

Dec 19, 2010 at 8:13 PM

Hi

I'm using visual basic and have added some layers but want to be able to change the order of already loaded layers without removing them and then adding them back in using - is there another way of moving a layer?

Chris

Developer
Dec 20, 2010 at 5:52 PM

Like any .Net style list, the typical method for re-ordering is to remove and insert in the desired location.  Just remember to call LockDispose() before you remove the layer and UnlockDispose() after you remove the layer or else the removal process will dispose the dataset.  Also, if you are worried about triggering update events, I think you can call "SuspendEvents" and "ResumeEvents" on the layers collection to prevent firing until you have the layers ordered the way you want them.  I don't know if that helps you, but otherwise, there doesn't seem to be an alternate way to reorder .net lists that I know of.

 

Ted

 

Dec 20, 2010 at 6:55 PM
Thanks, that works perfectly
Jun 22, 2011 at 7:45 AM

I realise the function as below:

public void moveUpLayer()
        {
            if (m_map.Layers.Count>0)
            {
                IMapLayer iMapLayer = GetSelectedLayer();
                if (iMapLayer==null)
                {
                    return;
                }
                MapLayerCollection mapLayerCollection = m_map.Layers as MapLayerCollection;
                mapLayerCollection.LockDispose();
                IMapLayer tempLayer = null;
                int SelectedLayerindex = mapLayerCollection.IndexOf(iMapLayer);
                for (int i = 0; i < mapLayerCollection.Count;i++ )
                {
                    if (SelectedLayerindex+1<mapLayerCollection.Count)
                    {
                        if (mapLayerCollection[SelectedLayerindex+1]==mapLayerCollection[i])
                        {
                            tempLayer = mapLayerCollection[i];
                            mapLayerCollection[i] = iMapLayer;
                            mapLayerCollection[i - 1] = tempLayer;
                            break;
                        }
                    }
                    else
                    {
                        MessageBox.Show("已到最顶层");
                    }
                }
                mapLayerCollection.UnlockDispose();
            }
            m_map.Refresh();
        }

 

Is someone can tell me the function above i write is correct?