You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

Add shapefile to map from listbox   Michael Harrop Aug 03, 2009
Re: Add shapefile to map from listbox   Paritosh Gupta Aug 03, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Add shapefile to map from listbox 
Author Michael Harrop 
Date Aug 03, 2009 
Message Hi,

I have a listbox with a list of shapefiles,
i want to be able to add which ever shapefile is selected to the Arcmap TOC

I've been using the sample cide to add shapefiles below

Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory

Dim pWorkSpace As IFeatureWorkspace
'Change C:\Source to the source location of the shapefile you wish to add
Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Source", 0)

Dim pClass As IFeatureClass
'Change USStates to the name of the shapefile you wish to add
Set pClass = pWorkSpace.OpenFeatureClass("USStates")

Dim pLayer As IFeatureLayer
Set pLayer = New FeatureLayer
Set pLayer.FeatureClass = pClass
pLayer.Name = pClass.AliasName

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

pMxDoc.AddLayer pLayer
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing



But what i want to do is amend this so instead of the shapefile name its uses the selected value of the listbox

Thanks
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Add shapefile to map from listbox 
Author Paritosh Gupta 
Date Aug 03, 2009 
Message Hi Michael
Just modify this line to accomodate ListBox.Text as : -

Set pClass = pWorkSpace.OpenFeatureClass(ListBox.Text)

This will facilitate to open the shapefile which has been selected in the listbox. 
 
Dim pWorkspaceFactory As IWorkspaceFactory 
Set pWorkspaceFactory = New ShapefileWorkspaceFactory 

Dim pWorkSpace As IFeatureWorkspace 
'Change C:\Source to the source location of the shapefile you wish to add 
Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Source", 0) 

Dim pClass As IFeatureClass 
'Change USStates to the name of the shapefile you wish to add 
Set pClass = pWorkSpace.OpenFeatureClass(ListBox.Text) 

Dim pLayer As IFeatureLayer 
Set pLayer = New FeatureLayer 
Set pLayer.FeatureClass = pClass 
pLayer.Name = pClass.AliasName 

Dim pMxDoc As IMxDocument 
Set pMxDoc = ThisDocument 

pMxDoc.AddLayer pLayer 
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing
 
  Paritosh Gupta
GIS Consultant
Eagle Technology
ESRI New Zealand
Mailto: paritosh.aps@gmail.com