How To: Add a layer to ArcMap using IGxDialog


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.



  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. 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()
    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

  5. Click the new UIButtonControl to execute the code that displays the shapefile browser dialog box.