Combobox FindString help

stramels

Newcomer
Joined
Oct 12, 2011
Messages
2
I am using VB.NET 2010 and having an issue with selecting the appropriate item in the combobox programmatically with info from the data base. The combobox contains the following items:

AAA
AA
A
B
C
D

I have verified that there are no leading or trailing spaces in the combobox items.

When the record in the database contains "A", the FindString finds the AAA record. If the database contains "B", "C" or "D", it finds the correct item.

Here is my code that gives an index of 0 to show "AAA:

Code:
lblSingles.Text = "A"  ' Simulate data from database

Dim index As Integer
index = cboSingles.FindString(lblSingles.Text)
cboSingles.SelectedIndex = index

Change "A" to "B" and the index is now 3 which is correct.

Code:
lblSingles.Text = "B"  ' Simulate data from database

Dim index As Integer
index = cboSingles.FindString(lblSingles.Text)
cboSingles.SelectedIndex = index

The user requires the combobox items to be listed in the order stated.

Any suggests are greatly appreciated!
 
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.findstring.aspx said:
Code:
FindString(String)
	Returns the index of the first item in the ComboBox that starts with the specified string.

FindString(String, Int32) 
	Returns the index of the first item in the ComboBox beyond the specified index that contains the specified string. The search is not case sensitive.

This behavior is correct. FindString doesn't search for an exact match, it searches for a string that starts with "A". You can loop over the items yourself though to search for a match.
Code:
[COLOR="Green"]-Pseudocode-[/COLOR]
Function GetItemIndex(ourString As String)
    For i = 0 to itemCount
        if items(i) = ourString Then return i
    Next

    Return -1 [COLOR="Green"]' indicates not found[/COLOR]
End Function
 
Back
Top