Select to view content in your preferred language

Problem selecting feature from ComboBox

2626
4
04-07-2010 05:26 PM
JasonWatkins
Emerging Contributor
Hello all

This is my first foray into the world of ArcObjects. As part of a form, I am trying to create a ComboBox that will select the feature associated with the text selected in the box. I have managed to populate the box with the NAME field from a shapefile of the USA. I have setup the WhereClause and SelectFeatures in the combobox sub but have a feeling that is where my error lies. When I use the created form and click on a state name nothing is selected but I am not given an error either. Where am I going wrong and thanks in advance for any help/advice?

Here is my code for a basic version of my form:


Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
    ' Clear the combobox & repopulate
    Me.ComboBox1.Clear
    Call GetValuesFromField
End Sub

Public Sub GetValuesFromField()
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = GetFeatureLayer("fe_2007_us_state") 'Or whatever the name of your shapefile is
    If pFeatureLayer Is Nothing Then
        Exit Sub
    End If
   
    Dim i As String
    i = pFeatureLayer.FeatureClass.Fields.FindField("NAME") 'Or whatever item you want to load into the listbox
    'Cursor through the sorted points and add the Str_name and StrNam_1 values to the
    'combobox.  Could also add the st type/suffix to the tablesort .fields if needed
    Dim pCursor As ICursor
    Dim pRow As IRow
   
    Dim pTSort As ITableSort
    Set pTSort = New TableSort
    With pTSort
      .Fields = "NAME"
      Set .Table = pFeatureLayer.FeatureClass
    End With
    pTSort.Sort Nothing
   
    Set pCursor = pTSort.Rows
    Set pRow = pCursor.NextRow
    Do Until pRow Is Nothing
      Me.ComboBox1.AddItem pRow.Value(i)
      Set pRow = pCursor.NextRow
    Loop


End Sub
Function GetFeatureLayer(strLayerName As String) As IFeatureLayer
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap

    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    'Find the layer
    Dim pLayer As ILayer
    Dim i As Integer
    For i = 0 To pMap.LayerCount - 1
        Set pLayer = pMap.Layer(i)
        If pLayer.Name = strLayerName Then
            Set GetFeatureLayer = pLayer
            Exit For
        Else
            Set GetFeatureLayer = Nothing
        End If
    Next i
End Function

Private Sub ComboBox1_Change()
  
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap
Dim pUSALayer As IFeatureSelection
Set pUSALayer = pMap.Layer(0)



Dim pQueryFilter As IQueryFilter             'Declares Query Filter Object
     Set pQueryFilter = New QueryFilter        'Sets object to a new filter
      
        pQueryFilter.WhereClause = "NAME = '" & ComboBox1.Text & "'"
        pUSALayer.SelectFeatures _
            pQueryFilter, esriSelectionResultNew, True
  
End Sub
0 Kudos
4 Replies
JasonWatkins
Emerging Contributor
I wanted to bump this to see if anyone had any ideas why this might not be working?
0 Kudos
JasonWatkins
Emerging Contributor
I got the combo box to select my feature by using this code:

Private Sub ComboBox1_Change()
  
Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pActiveView As IActiveView
  Dim pFeatureLayer As IFeatureLayer
  Dim pFeatureSelection As IFeatureSelection
  Dim pQueryFilter As IQueryFilter
 
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pActiveView = pMap
 
  'For simplicity sake let's use the first layer in the map
  If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
  Set pFeatureLayer = pMap.Layer(0)
  Set pFeatureSelection = pFeatureLayer 'QI
 
  'Create the query filter
  Set pQueryFilter = New QueryFilter
  pQueryFilter.WhereClause = "Name = '" & ComboBox1.Text & "'"
 
  'Invalidate only the selection cache
  'Flag the original selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
  'Perform the selection
  pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
  'Flag the new selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing

  
End Sub
0 Kudos
JasonWatkins
Emerging Contributor
Ok here is next problem that perhaps someone can help with. I have a text box that is being populated based on the selection from the combobox. The field I have connected to the textbox is a state code field (i.e. Colorado = CO).
However, when I select Colorado from the combo drop down the text box populates with a different records code. I figured out that this based on the FID of the record and there is some issue with the ListIndex but I don't know how to correct this problem. Here is the combobox code:

Private Sub ComboBox1_Change()
  
Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pActiveView As IActiveView
  Dim pFeatureLayer As IFeatureLayer
  Dim pFeatureSelection As IFeatureSelection
  Dim pQueryFilter As IQueryFilter
  Dim pFeature As IFeature

 
 
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pActiveView = pMap
 
  'For simplicity sake let's use the first layer in the map
  If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
  Set pFeatureLayer = pMap.Layer(0)
  Set pFeatureSelection = pFeatureLayer 'QI
  Set pFeature = pFeatureLayer.FeatureClass.GetFeature(ComboBox1.ListIndex)
 
 
 
  'Create the query filter
  Set pQueryFilter = New QueryFilter
  pQueryFilter.WhereClause = "Name = '" & ComboBox1.Text & "'"
 
  'Invalidate only the selection cache
  'Flag the original selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
  'Perform the selection
  pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
  'Flag the new selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing


    TextBox1.Text = pFeature.Value(2)
   
  
End Sub
0 Kudos
JasonWatkins
Emerging Contributor
I just wanted to report that I solved the last issue by calling the IEnumFeature. See my code below. I also added some additional textboxes with various values. My next step is to make these boxes editable so this can function as a data entry form. I'll post the results.

Private Sub ComboBox1_Change()

Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pQueryFilter As IQueryFilter
Dim pFeature As IFeature
Dim pEnumFea As IEnumFeature



Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pActiveView = pMap

'For simplicity sake let's use the first layer in the map
If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureSelection = pFeatureLayer 'QI
'Set pFeature = pFeatureLayer.FeatureClass.GetFeature(ComboBox1.ListIndex)



'Create the query filter
Set pQueryFilter = New QueryFilter
pQueryFilter.WhereClause = "Name = '" & ComboBox1.Text & "'"

'Invalidate only the selection cache
'Flag the original selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
'Perform the selection
pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
'Flag the new selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing

Set pEnumFea = pMap.FeatureSelection
Set pFeature = pEnumFea.Next
TextBox1.Text = pFeature.Value(2)
TextBox2.Text = pFeature.Value(4)
TextBox3.Text = pFeature.Value(5)
End Sub
0 Kudos