Select to view content in your preferred language

Populate listbox with attributes VB.Net

1715
7
12-15-2010 01:24 AM
MatthewPeterson
Emerging Contributor
Hi,

I am having trouble trying to populate a listbox with attributes on my vb.net windows form.

My shapefile has two fields, an address number and street name.  I need to use a query filter to search for an address number, and then populate my listbox with street names that have the same address number. 

I can't seem to get this to work.  Any help or pointers in the right direction is greatly appreciated!
0 Kudos
7 Replies
Udaya_BhaskerCheerala
Emerging Contributor
Pass layer index on form load then apply query filter to this layer and add reslult to list box when Find/OK button(assuming on form) is cliked.

Thanks,
Uday




Hi,

I am having trouble trying to populate a listbox with attributes on my vb.net windows form.

My shapefile has two fields, an address number and street name.  I need to use a query filter to search for an address number, and then populate my listbox with street names that have the same address number. 

I can't seem to get this to work.  Any help or pointers in the right direction is greatly appreciated!
0 Kudos
MatthewPeterson
Emerging Contributor
I can set the query filter up to search my attribute fields/select the correct street numbers, but how would I go about selecting the corresponding street names that are contained in a different field?

Also, what is the command for adding attributes to a list box using a query filter- I think this is where I am hung up at.

This is what I have so far, it crashes at:
lstboxStName.Items.Add(pQueryFilter.Values)
__________________________________________________________________

Dim pMxDoc As IMxDocument
pMxDoc = CType(m_app.Document, IMxDocument)
Dim pMap As IMap
pMap = pMxDoc.FocusMap

Dim pFlayer As IFeatureLayer
pFlayer = CType(pMap.Layer(0), IFeatureLayer)
Dim pFDef As IFeatureLayerDefinition
pFDef = pFlayer

Dim pFClass As IFeatureClass
pFClass = pFlayer.FeatureClass

Dim StreetNumber As Integer = txtStNum.Text
Dim pQueryFilter As IQueryFilter
pQueryFilter = New QueryFilter
pQueryFilter.WhereClause = "StreetNum='" & StreetNumber & "'"

lstboxStName.Items.Add(pQueryFilter.Values)
lstboxStName.Refresh()
0 Kudos
SteveFang
Deactivated User
Queryfilter actually return a cursor so you are heading down the wrong path.

Try this instead,  pretty basic code but hopefully get you in the right direction.

dim pFeaCursor as iFeaturecursor = pFClass.search(pqeryfilter, false)
dim pFeature as iFeature = pFeaCursor.nextfeature
dim streetname as string = ""

do until pfeature is nothing
   streetname = pfeature.value(pfeature.fields.findfield("Name of Street name field"))
   lstboxStName.Items.Add(streetname)
   pfeature = pfeacursor.nextfeature
loop
0 Kudos
MatthewPeterson
Emerging Contributor
Still having trouble with this.  Can't seem to get anything to work.

Queryfilter actually return a cursor so you are heading down the wrong path.

Try this instead,  pretty basic code but hopefully get you in the right direction.

dim pFeaCursor as iFeaturecursor = pFClass.search(pqeryfilter, false)
dim pFeature as iFeature = pFeaCursor.nextfeature
dim streetname as string = ""

do until pfeature is nothing
   streetname = pfeature.value(pfeature.fields.findfield("Name of Street name field"))
   lstboxStName.Items.Add(streetname)
   pfeature = pfeacursor.nextfeature
loop
0 Kudos
SteveFang
Deactivated User
What errors are you encountering in Visual Studio debugger?
0 Kudos
MatthewPeterson
Emerging Contributor
This is what I have, it dies at:
        Dim pFeaCursor As IFeatureCursor = pFeatLayer.Search(pQueryFilter, False)

I believe I need the query filter because I need to use an input box to get a street number the user enters.  Then select all the street numbers that match/partially match value.  The listbox is then populated with the street names that match/partially match the value in the input box.

I'm stumped...
______________________________________________________________________
  Dim pMap As IMap
        Dim pMxDoc As IMxDocument
        Dim pFeatLayer As IFeatureLayer
        Dim pFeatSel As IFeatureSelection
        Dim pQueryFilter As IQueryFilter
        pMxDoc = CType(m_app.Document, IMxDocument)
        pMap = pMxDoc.FocusMap

        ' Find Parcels Layer in TOC
        Dim sLayerName As String
        sLayerName = "Parcels"
        Dim i As Integer
        For i = 0 To pMap.LayerCount - 1
            If pMap.Layer(i).Name = sLayerName Then
                pFeatLayer = pMap.Layer(i)
                Exit For
            End If
        Next i

        'Check to See if Parcels is in TOC
        If pFeatLayer Is Nothing Then
            MsgBox("Please add the parcel shapefile to ArcMap")
            Exit Sub
        End If

        'Get Parcel ID
        Dim StNum As Integer
        StNum = txtStNum.Text

        ' Create the query filter
        pQueryFilter = New QueryFilter
        pQueryFilter.WhereClause = "StreetNum='" & StNum & "'"

        Dim pFeaCursor As IFeatureCursor = pFeatLayer.Search(pQueryFilter, False)
        Dim pFeature As IFeature = pFeaCursor.NextFeature
        Dim streetname As String = ""

        Do Until pFeature Is Nothing
            streetname = pFeature.Value(pFeature.Fields.FindField("StreetName"))
            lstboxStName.Items.Add(streetname)
            pFeature = pFeaCursor.NextFeature
        Loop

    End Sub



What errors are you encountering in Visual Studio debugger?
0 Kudos
SteveFang
Deactivated User
Your StNum variable is declared as integer yet in your queryfilter you are searching for a sting.  Is your street number field a numeric or string field?  If numeric then remove the single quote from your whereclause.  Plus your street name field in your code is "Street Na me".  I don't if you intend to have a space or not.  Maybe your code is not working due to nothing returning from the search.
0 Kudos