View Single Post
 
Old May 29th, 2015, 04:16 PM
jencarta jencarta is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

I started from scratch and defined the variables and initialized them in separate lines of code. I'm not sure what made the difference, but it worked this time. Here is the code I used for LayersOfMapButton.vb:

Code:
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Framework


Public Class LayersOfMapButton
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button

    Public Sub New()

    End Sub

    Protected Overrides Sub OnClick()
        Dim pData As String
        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        pData = ""
        pMxDoc = My.ArcMap.Application.Document
        pMap = pMxDoc.FocusMap
        'Using layer count property

        Dim pLayer As ILayer
        For i As Integer = 0 To pMap.LayerCount - 1
            pLayer = pMap.Layer(i)
            pData = pData + " >> " + pLayer.Name + vbNewLine
        Next
        pData = pData + String.Format("{0} Map contains {1} layers" + vbNewLine, pMap.Name, pMap.LayerCount)
        pLayer = Nothing

        pData = pData + "-------------------------" + vbNewLine
        pData = pData + "Using layers property" + vbNewLine
        Dim pEnumLayer As IEnumLayer
        pEnumLayer = pMap.Layers
        pLayer = pEnumLayer.Next()
        Dim j As Integer
        j = 0
        While pLayer IsNot Nothing
            j = j + 1
            pData = pData + " >> " + pLayer.Name + vbNewLine
            pLayer = pEnumLayer.Next()
        End While
        pData = pData + String.Format("{0} Map contains {1} layer(s)" + vbNewLine, pMap.Name, j)

        Dim msgForm As New Message()
        msgForm.lbl.Text = pData
        msgForm.ShowDialog()

    End Sub