If you set AutoCompleteMode = SuggestAppend
and AutoCompleteSource = ListItems
when a user type something, automatically the combobox show entries that starts with the typed characters.
Then by handling SelectedIndexChanged
or SelectedValueChanged
events, you will able to intercept when a user type exactly a value present in the values list.
If you also absolutely don't want user to type anything that's not in the list, well yes, you have to handle for example KeyDown
event like:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
char ch = (char)e.KeyValue;
if (!char.IsControl(ch))
{
string newTxt = this.comboBox1.Text + ch;
bool found = false;
foreach (var item in this.comboBox1.Items)
{
string itemString = item.ToString();
if (itemString.StartsWith(newTxt, StringComparison.CurrentCultureIgnoreCase))
{
found = true;
break;
}
}
if (!found)
e.SuppressKeyPress = true;
}
}