How do you prevent user input in a ComboBox so that only one of the items in the defined list can be selected by the user?
Even if the question is marked answered
, I would like to add some points
to it.
Set the DropDownStyle
property of the combobox to DropDownList
works for sure.
BUT what if the drop down list is longer, the user will have to scroll it to the desired item as he has no access to keyboard.
Private Sub cbostate_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cbostate.Validating
If cbostate.SelectedValue Is Nothing AndAlso cbostate.Text <> String.Empty Then
e.Cancel = True
MsgBox("Invalid State")
End If
End Sub
I did it like this. I wanted to restrict the user entering 'random values' instead of 'state' but keeping he should be able to type and search states.
This validating event
occurs when the control loses focus
. So if user enters wrong value
in combobox
, It will not allow user
to do anything on the form, perhaps it will not even allow to change the focus from the combobox