2 Replies Latest reply on Apr 30, 2012 12:45 PM by jtslbarnes

    ArcMap 9.2 VS2005 starting conversion from VBA to dotnet

    jtslbarnes
      Can anyone give me a good code example for a tool that will allow the user to select a feature and return the attribute information?

      Searching I notice there are big changes between versions 9.1, 9.2 and 10.  I am using VS2005 and 9.2 so please specific to these versions would be great.

      Thanks,

      John Barnes
        • Re: ArcMap 9.2 VS2005 starting conversion from VBA to dotnet
          Hornbydd
          John,

          Do you know about the EDN sites? These have a code sample section which you can filter for 9.2 and trawl to find examples. I think these sites have been "archived" by ESRI as they are for older versions of their software. Thankfully they have not removed them as there are a great number of people who still use older versions of their software, I for one am still actively using 9.1!

          The EDN site can be found here

          Duncan
          • Re: ArcMap 9.2 VS2005 starting conversion from VBA to dotnet
            jtslbarnes
            Evidently no could help me so I figured it out on my own.

            Not actually that much different than VBA.

            Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
                    'TODO: Add RetrunAttributes.OnClick implementation
                    'TODO: Add RetrunAttributes.OnMouseUp implementation

                    'Get project vital data
                    Dim pMxDoc As IMxDocument
                    Dim myApp As IMxApplication
                    myApp = m_application
                    pMxDoc = CType(m_application.Document, IMxDocument)

                    'Get the map and layer information
                    Dim pMap As IMap
                    pMap = pMxDoc.FocusMap
                    Dim pEnumLayer As IEnumLayer
                    pEnumLayer = pMap.Layers
                    pEnumLayer.Reset()
                    Dim pLayer As ILayer
                    pLayer = pEnumLayer.Next
                    Dim pFLayer As IFeatureLayer
                    Do While Not pLayer Is Nothing
                        If pLayer.Name = "tlots" And TypeOf pLayer Is IFeatureLayer Then ' Substitute your real layer name
                            pFLayer = TryCast(pLayer, IFeatureLayer)
                            Exit Do
                        End If
                        pLayer = pEnumLayer.Next
                    Loop


                    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                    'Get the users' clicked point
                    'Create an envelope at the point
                    'Select the feature at the clicked location with the envelope
                    Dim clickedPoint As IPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
                    Dim pEnvelope As Envelope = clickedPoint.Envelope
                    Dim pView As IActiveView = pMap
                    pEnvelope.Expand(pMxDoc.SearchTolerance, pMxDoc.SearchTolerance, False)
                    pMap.SelectByShape(pEnvelope, myApp.SelectionEnvironment, False)
                    pView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

                    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                    'Check to make sure the layer is available
                    If pFLayer Is Nothing Then
                        MsgBox("No Layer Found")
                        Exit Sub
                    End If

                    'Check to make sure a feature is selected
                    Dim pFSel As IFeatureSelection
                    pFSel = pFLayer
                    Dim pSelSet As ESRI.ArcGIS.Geodatabase.ISelectionSet2
                    pSelSet = pFSel.SelectionSet
                    If pSelSet.Count < 1 Then
                        MsgBox("No Features Selected")
                        Return
                    End If

                    Using comReleaser As ComReleaser = New ComReleaser() ' Recommended for cleaning up cursors
                        Dim pFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = Nothing
                        pSelSet.Search(Nothing, False, pFCursor)
                        comReleaser.ManageLifetime(pFCursor)
                        Dim pFeature As IFeature
                        pFeature = pFCursor.NextFeature ' Get First Feature
                        If pFeature Is Nothing Then
                            MsgBox("No Features Selected!") ' Just in case, but probably already taken care of earlier
                            Return
                        End If

                        'Return the appropriate attribute from the selected feature
                        Dim lFIndex As Long
                        lFIndex = pFCursor.FindField("Acad_Text") ' Replace with your field name
                        If lFIndex > -1 Then
                            Do While Not pFeature Is Nothing
                                fValue = pFeature.Value(lFIndex) 'I declare the fValue above the region
                                pFeature = pFCursor.NextFeature
                            Loop
                        End If

                        'Display the selected attribute
                        MsgBox(fValue)

                    End Using

                End Sub