How to add a layer to ArcMap using GxDialog


This example allows the user to select a feature dataset or feature class to be added to ArcMap using the GxDialog.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the Click event of a UIButtonControl in ArcMap.
[VBA]
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pGxDialog As IGxDialog
Set pGxDialog = New GxDialog
pGxDialog.AllowMultiSelect = True
pGxDialog.Title = "Select Feature Classes to Add To Map"

Dim pGxFilter As IGxObjectFilter
Set pGxFilter = New GxFilterFeatureClasses
Set pGxDialog.ObjectFilter = pGxFilter

Dim pGxObjects As IEnumGxObject
pGxDialog.DoModalOpen ThisDocument.Parent.hWnd, pGxObjects

If (pGxObjects Is Nothing) Then Exit Sub
pGxObjects.Reset

Dim pLayer As IFeatureLayer
Dim pGxDataset As IGxDataset
Set pGxDataset = pGxObjects.Next
Do Until (pGxDataset Is Nothing)
    Set pLayer = New FeatureLayer
    Set pLayer.FeatureClass = pGxDataset.Dataset
    pLayer.Name = pLayer.FeatureClass.AliasName
    pMxDoc.FocusMap.AddLayer pLayer
    Set pGxDataset = pGxObjects.Next
Loop
pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, Nothing