-1

I have a Combobox on my form for the user to select items in a drop-down box, also they are allowed to enter their own input. I need the user to add items to a combobox without adding a duplicate item already displayed.

For example: the combobox has a list of dog breeds such as (pug, boxer, pitbull). So how do I not allow the user to enter "pug" when they type that into the combobox?

I'm coding in Visual Basic, Thank You!!!!

MattC
  • 1
  • 1
  • 3
  • Loop through the combobox.items to see if the value is already in it – Keith Beard Apr 28 '15 at 23:09
  • So how would I not allow the user to enter said value, when the value is duplicated I need an exception thrown that stops it from being added. – MattC Apr 28 '15 at 23:23
  • providing some of the code being used might help people assist you – nomistic Apr 28 '15 at 23:27
  • You also might look into the auto complete list with the mode set to Append. That way if they type "pug" and pug is in the list, they will get as far as "pu" and the g will already be there. Also helpful to catch miss spells... – JStevens Apr 29 '15 at 00:38

2 Answers2

1

Use the code

ComboBox1.items.clear()

under your code

Til
  • 5,150
  • 13
  • 26
  • 34
0

i have tried it. This works.

And i am only assuming that u want an exception when duplicated value is entered. Following is the code and it is in the Combobox1.Validating event.

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

For more information about Validating event, search for yourself. Thanks!

Topman
  • 306
  • 1
  • 4
  • 14