LAST CALL 4 ADD LAYERS TO ARCMAP FROM LISTBOX

1839
4
01-15-2014 01:44 PM
JohnHanson
New Contributor
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.

PROBLEM:

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
            pMap.AddLayer(pGxLayer.Layer)
        Next
       

        ' 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)
        'Next

    End Sub
End Class
0 Kudos
4 Replies
LeoDonahue
Regular Contributor
Tell me what these lines of code are doing:


Dim pGxLayer As IGxLayer
Dim pGxFile As IGxFile

pGxLayer = New GxLayer
pGxFile = pGxLayer  ' In your words, what happens right here?

If you say: I'm setting pGxFile equal to pGxLayer, fine, but what does that mean?


For Each s As String In ListBox1.SelectedItems
  itemList.Add(s) ' what is this line for?
  pGxFile.Path = s
  pMap.AddLayer(pGxLayer.Layer)
Next


In your code sample, is pGxLayer and pGxFile referring to the same object?  You change the pGxFile path in each iteration of the For loop, but you're adding pGxLayer each time.  How is pGxLayer related to pGxFile changing the path?  Do you need to set something to null, or maybe create a new object reference?

I think it is very hard for people who learn VB to make the switch to C#  and Java mostly because VB hides alot of the Objectness of Object  Oriented programming, and for those of us who mainly work in those  languages, looking back at VB is also very hard.

What I don't like about VB is the way object references are handled for  you and the fact that the the object references are assigned with the  same operator that you use for equality testing, and the lack of wording  to distinguish between methods and properties, leaves me wondering why  anyone continues to use this language.
0 Kudos
NeilClemmons
Regular Contributor III
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.

PROBLEM:

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
            pMap.AddLayer(pGxLayer.Layer)
        Next
       

        ' 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)
        'Next

    End Sub
End Class


I would suggest using ILayerFile to open the layer file.  You're code is using an older and less intuitive method of working with layer files that was necessary before ILayerFile was added to the object model.
0 Kudos
JohnHanson
New Contributor
Mr. Donahue,

Thank you, for taking the time to examine the script.

Ques. #1:  pGxFile = pGxLayer  Should take the layer (.lyr) files and create them into objects.  This line of code and others were taken from the "Getting to Know ArcObjects (VBA)" pp. 303  which initially was provided as Set pGxFile = pGxLayer.  The "set" command is of VBA language and was taken out for VB.Net.

Ques. #2  itemList.Add(s)  creates an list/array of strings.  This snippet was derived from Mr. Clemmons suggestions from an older post.  I tried using "itemlist" and "s" as variables to extract the desired results, but keep getting the first layer (.lyr) only.  This line of code probably could be taken out and would still partially work, but wanted to keep it for possible manipulation.


Mr. Clemmons,

You probably recognize some of the code from an older post (http://forums.esri.com/Thread.asp?c=93&f=1170&t=291576) you provided for Mr. Shearer who had the same problem of getting only the first layer.  I never found the solution, or I was just unable figure it out.


My background:  I have experience in AML, Avenue, some VBA, some VB6, and now I am having to figure out VB.net 2010 and the new Add-in module.  I don't get much time to write code, in fact, it has been almost two years since I have attempted coding and it becomes difficult to get back to speed.

To both gentlemen:  I'll further investigate your pinpoints and would appreciate any suggestions on being able to select multiple layers from an listbox and add them to the ArcMap view document.

Regards,

John Hanson
0 Kudos
JohnHanson
New Contributor
Mr. Donahue,

Thank you, for taking the time to examine the script.

Ques. #1: pGxFile = pGxLayer Should take the layer (.lyr) files and create them into objects. This line of code and others were taken from the "Getting to Know ArcObjects (VBA)" pp. 303 which initially was provided as Set pGxFile = pGxLayer. The "set" command is of VBA language and was taken out for VB.Net.

Ques. #2 itemList.Add(s) creates an list/array of strings. This snippet was derived from Mr. Clemmons suggestions from an older post. I tried using "itemlist" and "s" as variables to extract the desired results, but keep getting the first layer (.lyr) only. This line of code probably could be taken out and would still partially work, but wanted to keep it for possible manipulation.

My background: I have experience in AML, Avenue, some VBA, some VB6, and now I am having to figure out VB.net 2010 and the new Add-in module. I don't get much time to write code, in fact, it has been almost two years since I have attempted coding and it becomes difficult to get back to speed.

I'll further investigate your pinpoints and would appreciate any suggestions on being able to select multiple layers from an listbox and add them to the ArcMap view document.

Regards,

John Hanson










Tell me what these lines of code are doing:


Dim pGxLayer As IGxLayer
Dim pGxFile As IGxFile

pGxLayer = New GxLayer
pGxFile = pGxLayer  ' In your words, what happens right here?

If you say: I'm setting pGxFile equal to pGxLayer, fine, but what does that mean?


For Each s As String In ListBox1.SelectedItems
  itemList.Add(s) ' what is this line for?
  pGxFile.Path = s
  pMap.AddLayer(pGxLayer.Layer)
Next


In your code sample, is pGxLayer and pGxFile referring to the same object?  You change the pGxFile path in each iteration of the For loop, but you're adding pGxLayer each time.  How is pGxLayer related to pGxFile changing the path?  Do you need to set something to null, or maybe create a new object reference?

I think it is very hard for people who learn VB to make the switch to C#  and Java mostly because VB hides alot of the Objectness of Object  Oriented programming, and for those of us who mainly work in those  languages, looking back at VB is also very hard.

What I don't like about VB is the way object references are handled for  you and the fact that the the object references are assigned with the  same operator that you use for equality testing, and the lack of wording  to distinguish between methods and properties, leaves me wondering why  anyone continues to use this language.
0 Kudos