arc obejcts - string builder

2902
4
Jump to solution
08-02-2015 01:22 AM
SusanneWagner
New Contributor II

Hello,

I'm newbie in vb.net and have question about string builder to show the arcgis layers. (System: ArcGis 10.2, VisualStudio2012) I don't succeed to show the loaded layers in a line of the message box.

Thanks for any help in advance!

code:

Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI
Imports System.Text

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

 
Public Sub New()

 
End Sub

 
Protected Overrides Sub OnClick()

  
AccessLayerViaMxDocument(My.ArcMap.Document)
  
My.ArcMap.Application.CurrentTool = Nothing
  
End Sub

  
Shared Sub AccessLayerViaMxDocument(ByVal pMxDocument As IMxDocument)
  
Dim pMap As IMap
  
Dim pMaps As IMaps = pMxDocument.Maps
  
Dim i As Integer

  
For i = 0 To pMaps.Count - 1 Step i + 1
  pMap
= pMaps.Item(i)
  
Debug.WriteLine(pMap.Name)
  
MsgBox(pMap.Name)

  
Dim pEnumLayer As IEnumLayer = pMap.Layers(Nothing, True)
  pEnumLayer
.Reset()
  
Dim pLayer As ILayer = pEnumLayer.Next()
  
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()


  
While Not pLayer Is Nothing

  
Debug.WriteLine(pLayer.Name)
  
' Show MessageBox with the name of the layer


  pLayer = pEnumLayer.Next()

  sb.Append(pLayer.Name)
  MsgBox(sb.ToString())

  End While

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

Susanne,

The reason you are only seeing 2 layers reported is because the sequence is incorrect your code should be:

Dim pEnumLayer As IEnumLayer = pMap.Layers(Nothing, True)
pEnumLayer.Reset()
Dim pLayer As ILayer = pEnumLayer.Next()
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
While Not pLayer Is Nothing
  sb.Append(pLayer.Name)
  pLayer = pEnumLayer.Next()
End While
MsgBox(sb.ToString())

To understand this you have created an enumerate and get the first layer, as this not nothing it enters the While loop. It appends the name and then gets the next layer. Your original code had the get next layer code before you ever added it to stringbuilder so this is why it skipped a layer name.

View solution in original post

4 Replies
DuncanHornby
MVP Notable Contributor

Susanne,

When you post code you should use the syntax highlighter in the Advance Editor option then >> button. This will make reading your code easier for others and improve your chances of getting an answer. You can go back and edit your question any time.

That being said looking at your code on each iteration of the While loop you append then print sb, so what is not working?

0 Kudos
SusanneWagner
New Contributor II

Hello Duncan,

thank you for your answer! I don't succeed to edit the code again but I will do it with next question! For code above he placement of the message box was wrong. It must be placed outside the loop. At least it works but when I put 4 layers in the arcgis toc there will be showed just 2 layers.....

0 Kudos
DuncanHornby
MVP Notable Contributor

Susanne,

The reason you are only seeing 2 layers reported is because the sequence is incorrect your code should be:

Dim pEnumLayer As IEnumLayer = pMap.Layers(Nothing, True)
pEnumLayer.Reset()
Dim pLayer As ILayer = pEnumLayer.Next()
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
While Not pLayer Is Nothing
  sb.Append(pLayer.Name)
  pLayer = pEnumLayer.Next()
End While
MsgBox(sb.ToString())

To understand this you have created an enumerate and get the first layer, as this not nothing it enters the While loop. It appends the name and then gets the next layer. Your original code had the get next layer code before you ever added it to stringbuilder so this is why it skipped a layer name.

SusanneWagner
New Contributor II

Dear Duncan,

thank you a lot!!! It works perfectly now!!