How To: Add a layer to ArcMap using IGxDialog
Note: This article pertains to ArcGIS versions 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
The GxDialog object is used to provide browser functionality to ArcMap or ArcCatalog. The IGxObjectFilter creates a filter to restrict the data types that appear in the browser.
Instructions provided describe how to add a shapefile to ArcMap using the GxDialog and a shapefile object filter.
Note: Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward
- Start ArcMap.
- Create a new UIButtonControl: How To: Create a new UIButtonControl
Note: For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
- Right-click the UIButtonControl and select View Source.
- Copy this code into the UIButtonControl's click event.
' add data showing shapefiles only in dialog ' posted by Jenny R. of ESRI Discussion Forum Private Sub UIButtonControl1_Click() AddShp End Sub Public Sub AddShp() 'make a dialog box object that will show shapefiles only Dim pGxDia As IGxDialog Set pGxDia = New GxDialog Dim pGxObFil As IGxObjectFilter 'the type of filter dictates what type of files can be chosen 'in this case it is shapefiles Set pGxObFil = New GxFilterShapefiles Set pGxDia.ObjectFilter = pGxObFil 'make a gxEnum object to hold the files that the user selects from the dialog box Dim gxEnum As IEnumGxObject If Not pGxDia.DoModalOpen(0, gxEnum) Then Exit Sub 'Exit if user hits Cancel End If Dim gxObj As IGxObject Set gxObj = gxEnum.Next 'Identify the workspace to access the file from 'in this case it will be a shapefile workspace factory since that is the type of file that will be chosen Dim wksFact As IWorkspaceFactory Set wksFact = New ShapefileWorkspaceFactory Dim wks As IWorkspace Set wks = wksFact.OpenFromFile(gxObj.Parent.FullName, 0) 'create a new feature class object from the selected workspace 'set it to the name of the shapefile chosen in the dialog Dim featWrk As IFeatureWorkspace Set featWrk = wks Dim fClass As IFeatureClass Set fClass = featWrk.OpenFeatureClass(gxObj.Name) 'make a feature layer from teh featureclass object Dim lyr As IFeatureLayer Set lyr = New FeatureLayer Set lyr.FeatureClass = fClass lyr.Name = gxObj.Name 'display the new feature layer on the current map Dim mxDoc As IMxDocument Set mxDoc = Application.Document Dim pMap As IMap Set pMap = mxDoc.FocusMap pMap.AddLayer lyr End Sub
- Click the new UIButtonControl to execute the code that displays the shapefile browser dialog box.