jwatkinstgs

Problem selecting feature from ComboBox

Discussion created by jwatkinstgs on Apr 7, 2010
Latest reply on Apr 16, 2010 by jwatkinstgs
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

Outcomes