Select a single feature

538
2
02-19-2011 01:17 PM
GagagDa_Morvi
New Contributor
I want to be able to select a single feature based upon where user clicks on the screen. How can I do this using ArcObjects? Thanks!
0 Kudos
2 Replies
TomGiles
New Contributor
I want to be able to select a single feature based upon where user clicks on the screen. How can I do this using ArcObjects? Thanks!


Hi, using vb, here is a main function which selects a feature (only one) from a selected layer.


Public Function Form_Initialize(ByVal m_application As ESRI.ArcGIS.Framework.IApplication) As Boolean
  Try
           
    pMxDoc = m_application.Document
           
    Dim pMap As IMap
    pMap = pMxDoc.FocusMap

    ' Get the selected layer
    Dim pLayer As IGeoFeatureLayer
    pLayer = pMxDoc.SelectedLayer


    ' Make sure a layer was selected
    If pLayer Is Nothing Then
      MsgBox("You must select a Layer to Attribute")
      Close()
      Exit Function
    End If

    ' Get the feature class to get the correct
    ' oid field name for the data type (oid,fid,objectid)
    'Dim pFc As IFeatureClass 'this is declared up top
   
    Dim strOIDName As String
    pFc = pLayer.FeatureClass
    strOIDName = pFc.OIDFieldName

    ' Get the selected features from the layer
    Dim pFSel As IFeatureSelection
    pFSel = pLayer
    Dim pSelSet As ISelectionSet
    pSelSet = pFSel.SelectionSet
    'Dim pFCur As IFeatureCursor 'declared up top
    pSelSet.Search(Nothing, False, pFCur)


    ' ensure only 1 feature selected
    If Not pSelSet.Count = 1 Then
       MsgBox("You must select one and only one feature.")
       Close() 'Exit Function
    Else
       'setup the combo boxes
       fillComboBoxes()
       pFeat = pFCur.NextFeature
       loadSegment(pFeat)

    End If

Catch ex As Exception
    MsgBox("Exception in Form_Initialize")
    System.Windows.Forms.MessageBox.Show(ex, "Initialize")
    Return False
  End Try
End Function

There's no idents, sorry.
Cheers,
Tom
0 Kudos
GagagDa_Morvi
New Contributor
Thanks, I get the idea for spatial selection from your post.
0 Kudos