I'm trying to get my code to check for duplicates in a combo box.
this is my code but I'm not sure where I'm going wrong
Private Sub flavorsComboBox_RegionChanged(sender As Object, e As EventArgs) Handles Me.Load
flavorsComboBox.Items.Add("Chocolate Almond")
flavorsComboBox.Items.Add("Espresso Roast")
flavorsComboBox.Items.Add("Jamaica Blue Mountain")
flavorsComboBox.Items.Add("Kona Blend")
flavorsComboBox.Items.Add("Vanilla Hazelnut")
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
Dim i As Integer = 0
Dim flavorlist As Integer = flavorsComboBox.Items.Count
Dim flavors As Integer = CInt(flavorsComboBox.Text)
Do While (i < flavorlist - 1)
If (flavorsComboBox.Items(i) = flavorlist) Then
MessageBox.Show("Error!")
End If
i += 1
Loop
Your bounds on your while loop are wrong, bracket use unusual, and I think that you're mixing your variables up somewhere along the line.
The following would iterate over a collection in a more readable fashion:
Dim newflavor As string = flavorsComboBox.Text
for each flavor as string in flavorsComboBox.Items
If flavor = newflavor Then
MessageBox.Show("Error!")
End If
next
You may need to use .ToLower on both sides of the equality operator though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.