Hide Legend in the TOC

579
2
05-15-2012 11:11 PM
AnikaMeyer
New Contributor
Dear all:

With the following code I would like to hide the legend in the TOC but it doesn't work. I wonder what I may do wrong. I am migrating my code from VBA (ArcGIS 9.2) to VB.Net in the Microsoft Visual Studio 2010 Programm.
I put you the new code. Perhaps you may help.
Thank you in advance.
Anika Meyer




            Dim pMXDoc As IMxDocument
            pMXDoc = My.ArcMap.Document

            Dim pMap As IMap
            pMap = pMXDoc.FocusMap

            Dim numLayer As Integer
            numLayer = pMap.LayerCount

            Dim pLayer As ILayer
            Dim pCadLayer As ICadDrawingLayers
            Dim pLegendInfo As ILegendInfo
            Dim pLegendGroup As ILegendGroup

            pLayer = pMap.Layer(1)

            pLayer.Visible = False
            If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
                pCadLayer = pLayer
                pLegendInfo = pCadLayer
                pLegendGroup = New LegendGroup
                pLegendGroup = pLegendInfo.LegendGroup(0)
                pLegendGroup.Visible = False
                'Else
                'MsgBox("Layer not found or invalid: " & sname)
            End If
0 Kudos
2 Replies
RichWawrzonek
Occasional Contributor
Try replacing the following three lines:

pLegendGroup = New LegendGroup
pLegendGroup = pLegendInfo.LegendGroup(0)
pLegendGroup.Visible = False


with this one:

LengendInfo.LegendGroup(0).Visible = False
0 Kudos
AnikaMeyer
New Contributor
Thank you very much Rich. But the problem seems to lie in the index of the legendgroup. For a CAD-Layer the legend is more complex. Trying the following code it now works.

                        For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
                            pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
                            pLegendGroup.Visible = False
                        Next intLegendIndex

Kind regards,
Anika

The whole code looks like this:

    Public Sub LeyendasVisibilidadForum()
        Try
            Dim pMXDoc As IMxDocument
            pMXDoc = My.ArcMap.Document

            Dim pMap As IMap
            pMap = pMXDoc.FocusMap

            Dim numLayer As Integer
            numLayer = pMap.LayerCount

            Dim pLayer As ILayer
            Dim pCadLayer As ICadDrawingLayers
           
            Dim pLegendInfo As ILegendInfo
            Dim pLegendGroup As ILegendGroup

            Dim i As Integer
            Dim intLegendIndex As Integer

            Dim strPolygon1 As String = "*Polygon*"
            Dim strPolygon3 As String = "*Point*"
            Dim strPolygon4 As String = "*Polyline*"
            Dim strPolygon5 As String = "*MultiPatch*"
            Dim strPolygon6 As String = "toponaa1"

            For i = 0 To numLayer - 1

                pLayer = pMap.Layer(i)

                If pLayer.Name Like strPolygon5 Then
                    pLayer.Visible = False
                    If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
                        pCadLayer = pLayer
                        pLegendInfo = pCadLayer
                        For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
                            pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
                            pLegendGroup.Visible = False
                        Next intLegendIndex
                    End If
                ElseIf pLayer.Name Like strPolygon1 Then
                    pLayer.Visible = False
                    If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
                        pCadLayer = pLayer
                        pLegendInfo = pCadLayer
                        For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
                            pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
                            pLegendGroup.Visible = False
                        Next intLegendIndex
                    End If
                ElseIf pLayer.Name Like strPolygon3 Then
                    pLayer.Visible = False
                    If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
                        pCadLayer = pLayer
                        pLegendInfo = pCadLayer
                        For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
                            pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
                            pLegendGroup.Visible = False
                        Next intLegendIndex
                    End If
                ElseIf pLayer.Name Like strPolygon4 Then
                    If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
                        pCadLayer = pLayer
                        pLegendInfo = pCadLayer
                        For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
                            pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
                            pLegendGroup.Visible = False
                        Next intLegendIndex
                    End If
                ElseIf pLayer.Name Like strPolygon6 Then
                    pLayer.Visible = False
                End If
            Next
            pMXDoc.UpdateContents()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
0 Kudos