[GIS] Displaying selected features in ArcGIS Identify Dialog using Visual Studio 2010/ArcObjects

add-inarcgis-10.2arcobjectsvisual studio

I'm brand new to ArcObjects SDKs and am struggling. I have a Python Add-in performing a query to select records (works great)and now trying to call the identify dialog via an .NET addin button that displays the identify dialog box to show attributes of the selected records. Below is the code I have at this point. I currently have the identify dialog displaying, but no records appearing. I know I need to input the selected records somewhere….but not sure where.

I'm using Visual Studio/Microsoft Visual Basic 2010 and ArcGIS 10.2.1.

Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto

Public Class Identify_Button
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pMxDoc As IMxDocument
Dim activeView As IMap

Public Sub DoIdentify(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal x AsSystem.Int32, ByVal y As System.Int32)
pMxDoc = My.ArcMap.Application.Document
activeView = pMxDoc.FocusMap
If activeView Is Nothing Then
    Return
End If

Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass
identifyDialog.Map = map

'Clear the dialog on each mouse click
identifyDialog.ClearLayers()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay

Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay ' Implicit Cast
identifyDialog.Display = display

Dim identifyDialogProps As ESRI.ArcGIS.CartoUI.IIdentifyDialogProps = CType(identifyDialog, ESRI.ArcGIS.CartoUI.IIdentifyDialogProps) ' Explicit Cast
Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = identifyDialogProps.Layers
enumLayer.Reset()

Dim layer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next

Do While Not (layer Is Nothing)
    identifyDialog.AddLayerIdentifyPoint(layer, x, y)
    layer = enumLayer.Next()
Loop

identifyDialog.Show()

End Sub
Public Sub New()

End Sub

Protected Overrides Sub OnClick()
    DoIdentify(activeView, 300, 100)
End Sub

Protected Overrides Sub OnUpdate()
    Enabled = My.ArcMap.Application IsNot Nothing
End Sub

Best Answer

This was a struggle, and I'm sure there is a classier way, but this is the code that I finally got working. The only glitch is that the identify box expands the first layer (so you can see all selected records), but the second layer stays condensed. You have to manually click the "+" to expand after the script runs.

Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.CartoUI
Imports ESRI.ArcGIS.Geodatabase

Public Class ShowSelectedWR
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pMxDoc As IMxDocument
Dim activeView As IMap

Sub test()
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim pIdentifyDialog As IIdentifyDialog
    Dim pIdentifyDialogProps As IIdentifyDialogProps
    Dim pEnumLayer As IEnumLayer
    Dim pLayer As ILayer2
    Dim pFtrLyr As IFeatureLayer
    Dim pFtrSel As IFeatureSelection
    Dim pFtrCsr As IFeatureCursor
    Dim pFtr As IFeature

    pMxDoc = My.ArcMap.Application.Document
    pActiveView = pMxDoc.FocusMap
    pIdentifyDialog = New IdentifyDialog
    pIdentifyDialogProps = pIdentifyDialog
    pIdentifyDialog.Map = pMxDoc.FocusMap
    pIdentifyDialog.Display = pActiveView.ScreenDisplay

    pIdentifyDialog.ClearLayers()

    pEnumLayer = pIdentifyDialogProps.Layers

    pEnumLayer.Reset()
    pLayer = pEnumLayer.Next

    ' Get a ref to the first layer
    pFtrLyr = pMxDoc.FocusMap.Layer(0)

    ' Get a cursor on the selected features in this layer
    pFtrSel = pFtrLyr
    ' Sets cursor to start at beginning of selected feature
    pFtrSel.SelectionSet.Search(Nothing, False, pFtrCsr)

    ' loop thru selected features and display OID of each

    pFtr = pFtrCsr.NextFeature
    While Not pLayer Is Nothing
        Do While Not pFtr Is Nothing
            'MsgBox("Before pFtr NextFeature Call")
            pIdentifyDialog.AddLayerIdentifyOID(pLayer, pFtr.OID)
            Debug.Print(pFtr.OID)
            pFtr = pFtrCsr.NextFeature
            'MsgBox("End of First Loop")
        Loop
        ' Loop to move through layers and NOT call identify loop unless 
        ' there are selected records.
        pLayer = pEnumLayer.Next
        If Not pLayer Is Nothing Then
            pFtrLyr = pLayer
            pFtrSel = pFtrLyr
            pFtrSel.SelectionSet.Search(Nothing, False, pFtrCsr)
            Debug.Print(pFtrLyr.Name)
            pFtr = pFtrCsr.NextFeature
        End If
    End While
    pIdentifyDialog.Show()
End Sub

Public Sub New()

End Sub

Protected Overrides Sub OnClick()
    test()
End Sub

Protected Overrides Sub OnUpdate()
    Enabled = My.ArcMap.Application IsNot Nothing
End Sub
End Class