stramels Posted October 12, 2011 Posted October 12, 2011 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! Quote
Leaders snarfblam Posted October 12, 2011 Leaders Posted October 12, 2011 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 Quote [sIGPIC]e[/sIGPIC]
stramels Posted October 13, 2011 Author Posted October 13, 2011 Thanks, snarfblam. I appreciate your assistance! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.