0

If I add a shapefile to my map and then a dynamic custom layer inherited from a BaseDynamicLayer, then the shapefile 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 Shapefile, the shapefile is makes the dynamic layer non viewable because it is higher on the viewing stack...

mapControl.AddLayer(dynamicCustomLayer); 
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 shapefile after the custom layer was enabled...

I'm using C# ArcEngine 10

2 Answers 2

1

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.

5
  • I was going to answer the same but how do you catch when the layer get's added? Commented Sep 9, 2010 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
    Commented Sep 9, 2010 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. Commented Sep 9, 2010 at 19:22
  • nevermind I found the event. Commented Sep 9, 2010 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
    Commented Sep 9, 2010 at 20:11
1

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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