How to sort items in combobox?

3026
4
05-01-2013 11:54 AM
geomatic
New Contributor
Hi Everyone,

I want to ask spesific question about sorting items in combobox from A to Z. How can i perform sorting them?

If i cant sort them A to Z there is meaningless about the query that will be applied on it.

Waiting forward for your replies, kindly regards.

Tolga

Here are my vba codes:

Private Sub UserForm_Initialize()

Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pFeatLayer As IFeatureLayer
Dim pFeatClass As IFeatureClass
Dim pFields As IFields
Dim pField As IField
Dim i As Integer

Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
Set pFeatLayer = pMap.Layer(0)
Set pFeatClass = pFeatLayer.FeatureClass
Set pFields = pFeatClass.Fields

For i = 0 To pFields.FieldCount - 1
cboMusteri.AddItem pFields.Field(i).Name


Next

Dim pLayer As ILayer
Set pLayer = pMap.Layer(0)
Set pFeatLayer = pLayer
Dim pFeature As IFeature
Dim pFeatCursor As IFeatureCursor
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
pQueryFilter.SubFields = "MALIK"
Set pFeatCursor = pFeatLayer.Search(pQueryFilter, True)
Set pFeature = pFeatCursor.NextFeature

Dim j As Integer
Dim cDuplicate As Boolean
cDuplicate = False

cboMusteri.Clear

Do While Not pFeature Is Nothing
For j = 0 To cboMusteri.ListCount - 1
cDuplicate = False
If cboMusteri.List(j) = pFeature.Value(pFeature.Fields.FindField("MALIK")) & "" Then
cDuplicate = True
End If
Next j

If cDuplicate = False Then
cboMusteri.AddItem pFeature.Value(pFeature.Fields.FindField("MALIK")) & ""
End If
Set pFeature = pFeatCursor.NextFeature

Loop

Set pFeatCursor = Nothing
Set pFeature = Nothing
Set pFeatLayer = Nothing
Set pLayer = Nothing
Set pMap = Nothing
Set pMxDoc = Nothing
End Sub
0 Kudos
4 Replies
LanceShipman
Esri Regular Contributor
0 Kudos
JimMeuse
New Contributor
Maybe try something like this:

pQueryFilterDefinition.PostfixClause = "ORDER BY Town"

using IQueryFilterDefinition2 interface.
0 Kudos
DuncanHornby
MVP Notable Contributor
Jim's solution is good but it does not work with all data formats (i.e. Shapefiles) see this thread.

Maybe try something like this:

pQueryFilterDefinition.PostfixClause = "ORDER BY Town"

using IQueryFilterDefinition2 interface.
0 Kudos
TomaCasa
New Contributor III
just throw them into a list of strings first?

I use c# to code in, but its pretty simple in both languages.

using system.Collections.Generic;

List<String> cblist = new List<String>();
cblist.Items.Add(Value);
cblist.Sort();

then you can either bind the list to the Combo box, or manually add them in by looping 🙂

Tomas.
0 Kudos