Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I add a shape file to my map and then a dynamic custom layer inherited from a BaseDynamicLayer, then the shape file is lower on the viewing stack then my custom layer, and it displays correctly.

mapControl.AddShapeFile(directoryPath, shapeFile);
mapControl.AddLayer(dynamicCustomLayer); //this is ok

But if I add my dynamicCustomLayer first, and then add a Shape file, the shape file is makes the dynamic layer non viewable because it is higher on the viewing stack...

mapControl.AddShapeFile(directoryPath, shapeFile);//now my dynamic layer is invisible

How can I ensure that the dynamic custom layer is always on top of the viewing stack even if my user adds a shape file after the custom layer was enabled... Thanks in advance :)

I'm using C# arcengine 10

share|improve this question
up vote 1 down vote accepted

You'll have to keep a reference to your dynamic layer, then just use IMapControl2.MoveLayerTo, which takes a from index and a to index, both 0 based. Smaller numbers draw later, so it sounds like you need to keep this at index 0.

Edit: You don't need a reference to the layer, unless you can't keep track of what index it's on and have to iterate through the Layer collection. Adding a shapefile will automatically put the shapefile at index 0 and promote everything else by 1. If the layer you need to keep on top was at 0 before, you know it's at 1 now. Makes it a bit easier to manage.

See ESRI's documentation for MoveLayerTo.

share|improve this answer
I was going to answer the same but how do you catch when the layer get's added? – MathiasWestin Sep 9 '10 at 19:01
I'd assumed that he was in control of things being added - it's ArcEngine. In ArcMap, you'd have to catch the event when someone added data to the TOC, but here, you've got control of it. – Herb Sep 9 '10 at 19:16
depends on how you do it, there is add layer command that esri created fo you that can be added to the toolbar control. In that case you don't have control over when the user adds the layer. – MathiasWestin Sep 9 '10 at 19:22
nevermind I found the event. – MathiasWestin Sep 9 '10 at 19:48
I assume you'd have to get IActiveView and subscribe to IActiveViewEvents.ItemAdded. Details are sketchy (ok, non-existent) w/o firing up a compiler, though. – Herb Sep 9 '10 at 20:11

If you need to catch that the user adds layer you can wire up a ItemAdded event on the active view:

    Private activeViewEvents As ESRI.ArcGIS.Carto.IActiveViewEvents_Event
    Private m_ActiveViewEventsItemAdded As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler

    Private Sub AddEventListenerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddEventListenerToolStripMenuItem.Click
        activeViewEvents = CType(axMapControl1.ActiveView.FocusMap, ESRI.ArcGIS.Carto.IActiveViewEvents_Event)

        m_ActiveViewEventsItemAdded = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler(AddressOf OnActiveViewEventsItemAdded)

        AddHandler CType(axMapControl1.ActiveView.FocusMap, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemAdded, m_ActiveViewEventsItemAdded
    End Sub

    Private Sub OnActiveViewEventsItemAdded()
        'makes layer(1) always on top.
        axMapControl1.ActiveView.FocusMap.MoveLayer(axMapControl1.ActiveView.FocusMap.Layer(0), 1)
    End Sub 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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