Dim pGxLayer As IGxLayer Dim pGxFile As IGxFile pGxLayer = New GxLayer pGxFile = pGxLayer ' In your words, what happens right here?
For Each s As String In ListBox1.SelectedItems itemList.Add(s) ' what is this line for? pGxFile.Path = s pMap.AddLayer(pGxLayer.Layer) Next
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.SystemUIImports System.Windows.FormsImports SystemImports ESRI.ArcGIS.esriSystemImports ESRI.ArcGIS.FrameworkImports ESRI.ArcGIS.CartoImports ESRI.ArcGIS.ArcMapUIImports ESRI.ArcGIS.CatalogImports ESRI.ArcGIS.GeodatabaseImports ESRI.ArcGIS.DataSourcesFileImports System.Windows.Forms.ListBoxPublic 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 SubEnd Class
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.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.