VB6 Finding a combobox listindex based off of a specific string

10,655

this should work:

Sub FindComboIndex(ByVal cmbDealerName As ComboBox, ByVal result As String)
    Dim i As Integer
    For i = 0 To cmbDealerName.ListCount - 1
        If result = cmbDealerName.List(i) Then
            cmbDealerName.ListIndex = i
            Exit Sub
        End If
    Next i
End Sub

EDIT: fixing code since it wasn't tested and did not work. Code above now works

Share:
10,655
Zingo
Author by

Zingo

Updated on June 04, 2022

Comments

  • Zingo
    Zingo almost 2 years

    I have a combobox with many items on the list. I just need to find the index of a specific string and have the combobox change to that index. I have a query that returns a result as string.

     Function FindComboIndex(cmbDealerName,result)
    
     For int i = 0 to cmbDealerName.ListItems.Count
    
     If result = cmbdealername.ListItem(i).Text Then
    
     cmbdealername.listindex = i
    
     End if
    
     Next i
    
     End function
    

    Something like this, i just need the right syntax

  • C-Pound Guru
    C-Pound Guru about 10 years
    Added an Exit Sub if the string is found so it doesn't keep looping.