Discussion created by jhanson5874 on Jan 15, 2014
Latest reply on Jan 16, 2014 by jhanson5874
I am creating an Add-in with ArcGIS 10.1 using MS Visual Studio 2010 VB.net

I have a button on my toolbar that opens a form (frmFieldDescription) with a listbox (ListBox1) that is populated with a list of .lyrs from a directory (G:\PPACG\)......this works fine.


On the form I created another button (Button1_Click).....I've been trying to get this button to load the selected .lyr(s) from the listbox to be loaded onto ArcMap's View Document.

Currently, with the code I have under (Button1_Click) works but it will it will only add the first .lyr  For example, if I select four different layers it will only load the first selected layer (i.e. airports) four times into the ArcMap View Document.  I tried using
MsgBox(s.ToString, , "Oops") and it returns the correct four different layers.

QUESTION:  How can I iterate thru "s" to bring in the correct four different layers into ArcMap?

See the code below for Button1_Click:

Imports ESRI.ArcGIS.SystemUI
Imports System.Windows.Forms
Imports System
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Catalog
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.DataSourcesFile
Imports System.Windows.Forms.ListBox

Public Class frmFieldDescription

    Private Sub frmFieldDescription_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim directoryInfo As IO.DirectoryInfo = New IO.DirectoryInfo("G:\PPACG\CADASTRAL_LYRS")

        If Not (directoryInfo Is Nothing) Then
            Dim fileInfo As IO.FileInfo() = directoryInfo.GetFiles
            Dim i As Int32
            For i = 0 To fileInfo.GetUpperBound(0)
                ListBox1.Items.Add(fileInfo(i).FullName)   'JH Changeback to Name
            Next i
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '''''''''''''''''''''''''''MsgBox(Mychoice.ToString, , "Oops")
        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        pMxDoc = My.ArcMap.Document
        pMap = pMxDoc.FocusMap
        Dim pGxFile As IGxFile
        Dim pGxLayer As IGxLayer
        pGxLayer = New GxLayer
        pGxFile = pGxLayer
        Dim itemList As New List(Of String)
        'Dim Mychoice As Integer

        For Each s As String In ListBox1.SelectedItems

            itemList.Add(s)   '''''itemlist does not have need return   "s" does return the correct string
            ''''' MsgBox(s.ToString, , "Oops")

            pGxFile.Path = s

        ' Dim itemArr() As String = itemList.ToArray

        'For Each s As String In itemArr

        'pGxFile.Path = s
        ' pMap.AddLayer(pGxLayer.Layer)

        '''''''''''''''''''''''' System.Windows.Forms.MessageBox.Show(s)

    End Sub
End Class