3

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;
        axTOCControl1.Refresh();

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

2 Answers 2

2

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()

0

Renee,

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?

2
  • 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: Commented Mar 10, 2011 at 13:37
  • aMap.MoveLayer(pLayer, nLyrs - 1); INALayer2 anNALyr = (INALayer2)pLayer; anNALyr.Expanded = false; //Refresh the TOC and the map window activeView.ContentsChanged(); Commented Mar 10, 2011 at 13:38

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.