我的表单上有一个Combobox,供用户在下拉框中选择项目,也允许他们输入自己的输入。我需要用户将项目添加到组合框而不添加已显示的重复项目。
例如:组合框有一个狗品种列表,如(哈巴狗,拳击手,比特犬)。那么当他们在组合框中输入“pug”时,我怎么不允许用户输入“pug”?
我在Visual Basic中编码,谢谢!!!!
我试过了。这有效。
我只是假设你在输入重复值时想要一个例外。以下是代码,它位于Combobox1.Validating事件中。
Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
For Each item As String In ComboBox1.Items
If item.ToLower.Contains(ComboBox1.Text.ToLower) Then
MsgBox("Duplicate value: there's already '" & item & "' in the list. Please select from the list.", MsgBoxStyle.Exclamation, "Dog Breed")
ComboBox1.Text = ""
Exit For
End If
Next
End Sub
有关验证事件的更多信息,请搜索自己。谢谢!
使用代码
ComboBox1.items.clear()
在你的代码下