Select to view content in your preferred language

Select Layer in TOC using VBA

1685
4
Jump to solution
03-07-2012 02:52 AM
LopeLorenzo
Emerging Contributor
Hi there!

I am trying to have a layer in the TOC selected by its name. Say I have a layer named "Roads", I would like to have this layer selected in the TOC, but I don't want to check its view, just have it selected (in the same as when you click on a layer and it turns blue and remains selected). ¿Is it possible?

Thank you!

Here you are a piece of vba:

Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMapa As IMap
Set pMapa = pDoc.FocusMap
Dim pCapa As ILayer
Set pCapa = pDoc.SelectedLayer

If pCapa Is Nothing Then
MsgBox "No layers selected"
MsgBox pMapa.Layer(0).Name 'First layer name in the TOC
'How do I select this layer?
Else
MsgBox pCapa.Name 'Layer selected
If pCapa.View = False then
pCapa.View = True 'It turn on its view but I would like tu select it
End if
End If
0 Kudos
1 Solution

Accepted Solutions
HarishDave
Emerging Contributor
Here it goes...


    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument

    Dim pMap As IMap
    Set pMap = pDoc.FocusMap
   
    pDoc.ContentsView(0).AddToSelectedItems pMap.Layer(0)         ' selects first layer in TOC
    pDoc.ContentsView(0).Refresh (0)      ' Refresh TOC


Cheers.....

View solution in original post

0 Kudos
4 Replies
NeilClemmons
Honored Contributor
You can select a layer by setting IContentsView::SelectedItem.  You can get the contents view reference from IMxDocument::CurrentContentsView.
0 Kudos
HarishDave
Emerging Contributor
Here it goes...


    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument

    Dim pMap As IMap
    Set pMap = pDoc.FocusMap
   
    pDoc.ContentsView(0).AddToSelectedItems pMap.Layer(0)         ' selects first layer in TOC
    pDoc.ContentsView(0).Refresh (0)      ' Refresh TOC


Cheers.....
0 Kudos
LopeLorenzo
Emerging Contributor
Thank you very much to Neil and HPSoln, you two hitted the spot!

Here I leave you another option I found on internet.

And please, sorry for the delay 😕

Regards!

Sub SelectLayersInDisplayView()

' selects all of the layers in the first data frame in the TOC's display view

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pTOC As IContentsView
    Set pTOC = pMxDoc.ContentsView(0) ' Display View
  
    Dim pMaps As IMaps
    Set pMaps = pMxDoc.Maps
    Dim pMap As IMap
  
    Dim i As Integer
    Set pMap = pMaps.Item(0) ' first data frame
    Dim pEnumLayer As IEnumLayer
    Set pEnumLayer = pMap.Layers(, False) ' we will not select layers within group layers
    Dim pLayer As ILayer
    Set pLayer = pEnumLayer.Next
  
    pTOC.RemoveFromSelectedItems pTOC.SelectedItem
    Do While Not pLayer Is Nothing
        pTOC.AddToSelectedItems pLayer
        pTOC.Refresh pLayer
        Set pLayer = pEnumLayer.Next
    Loop
  
End Sub

Sub SelectDataFrameInSourceView()

' selects the first data frame in the TOC's source view

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pTOC As IContentsView
    Set pTOC = pMxDoc.ContentsView(1) ' Source View
  
    Dim pMaps As IMaps
    Set pMaps = pMxDoc.Maps
    Dim pMap As IMap
    Set pMap = pMaps.Item(0) ' first data frame
      
    pTOC.SelectedItem = Nothing
    pTOC.SelectedItem = pMap
    pTOC.Refresh pMap
End Sub
0 Kudos
LopeLorenzo
Emerging Contributor
Here it goes...


    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument

    Dim pMap As IMap
    Set pMap = pDoc.FocusMap
   
    pDoc.ContentsView(0).AddToSelectedItems pMap.Layer(0)         ' selects first layer in TOC
    pDoc.ContentsView(0).Refresh (0)      ' Refresh TOC


Cheers.....


One more question, it would be possible to move the scroll to show you the selected layer (in case you have many layers and you are not seeing de first one)?.
0 Kudos