I have created a toolbar add in for arc with a button that opens a form with 3 selections that are shapefiles, once the shapefile is selected I click the Add Layer button. Currently I can only get one shapefile at a time, unless I duplicate the code for each shp and once I do add the one shapefile it keeps on adding it. Here's the code I have so far:
Public Function AddLayers()
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
' Specify the workspace and the feature class.
pWorkspaceFactory = New ShapefileWorkspaceFactory
pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("E:\Customization\Data\Cities", 0)
pFeatureClass = pFeatureWorkspace.OpenFeatureClass("Cities")
' Prepare a feature layer.
pFeatureLayer = New FeatureLayer
pFeatureLayer.FeatureClass = pFeatureClass
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
' Add the feature layer to the active map.
pMxDoc = My.ArcMap.Application.Document
pMap = pMxDoc.FocusMap
pMap.AddLayer(pFeatureLayer)
' Refresh the active view.
pMxDoc.ActiveView.Refresh()
Return AddLayers()
End Function
Best Answer
First of all you have declared a Function yet you return no result. You should be declaring this a Sub
Your very last line is Return AddLayers() I believe this is the problem. You are returning a call to the function itself so I suspect it is entering the function which then enters itself ad infinitum.
So turn this into a Sub and remove the last line of code.