我试图让我的代码检查组合框中的重复项。
这是我的代码,但我不知道我哪里出错了
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
答案 0 :(得分:1)
你的while循环中的界限是错误的,括号使用不正常,我认为你是将你的变量混合到某个地方。
以下内容将以更易读的方式迭代集合:
Dim newflavor As string = flavorsComboBox.Text
for each flavor as string in flavorsComboBox.Items
If flavor = newflavor Then
MessageBox.Show("Error!")
End If
next
您可能需要在相等运算符的两侧使用.ToLower。