I have the following snippit of code to perform this. However, it doesn't do anything to the layer in the TOC on the map that gets generated from this code. When the layer is created and the map shows up, the layer appears expanded. I want it to collapse. Am I doing something wrong? Thanks.

        IMap aMap = axMapControl1.ActiveView.FocusMap;
        ILayer pLayer = aMap.get_Layer(0);
        INALayer2 anNALyr = (INALayer2)pLayer;
        anNALyr.Expanded = false;

Also, how do I get a layer in the TOC to move to the bottom of the TOC?


You're question is similar to mine: Programmatically uncheck layer in AxTOCControl

The solution was to use the correct refresh method: IActiveView.ContentsChanged

For layer movement, use mapcontrol.map.MoveLayer()



Here is the VB version of how to move a layer to the bottom of the TOC.

'Move layer in the TOC

 Dim pMxDocument As IMxDocument

  Dim pContentsView As IContentsView

  Dim pMap as IMap

  Set pMxDocument = Application.Document

  Set pContentsView = pMxDocument.CurrentContentsView

  Dim pFocusMap As IMap

  Dim Layer1 As ILayer

  Dim Layer2 As ILayer

  Set pFocusMap = pMxDocument.FocusMap

  Set Layer1 = pFocusMap.Layer(0)

  Dim LayerSet As esriSystem.ISet

  Set LayerSet = New esriSystem.Set

  LayerSet.Add Layer1

  pContentsView.SelectedItem = LayerSet

  Dim i As Integer

  Dim pLayer As ILayer

  Set pMxDocument = Application.Document

  Set pMap = pMxDocument.FocusMap

  Set pLayer = pMxDocument.SelectedLayer

Dim ii As Long

Dim layerIndex As Long

For ii = 0 To pMap.LayerCount - 1

If pMap.Layer(i).name = pLayer.name Then

layerIndex = ii

End If

Next ii

pMap.MoveLayer pLayer, layerIndex - 1

Are you trying to collapse a single layer or a group layer?

  • Thank you. I'm all set with this now. I was able to accomplish what I needed to do in terms of moving and collapsing the layer in the TOC with the following code: – Renee Cammarere Mar 10 '11 at 13:37
  • aMap.MoveLayer(pLayer, nLyrs - 1); INALayer2 anNALyr = (INALayer2)pLayer; anNALyr.Expanded = false; //Refresh the TOC and the map window activeView.ContentsChanged(); – Renee Cammarere Mar 10 '11 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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