Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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.

 

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!

  • Leaders
Posted
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.

[/Quote]

 

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.

[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

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...