Reputation:
I have Combo Box
and i want to make sure that a user cannot type anything outside the letters in the Combo Box
. I have tried handling the Key Down
Event of the Combo Box
but it doesn't work.
This is what i have tried
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
string regexString = "^[A-Z]";
Match matches = Regex.Match(comboBox1.Text, regexString);
if (!matches.Success)
{
e.SuppressKeyPress = true;
comboBox1.SelectedIndex = 0;
}
}
Please is there any better way to do this?
Upvotes: 0
Views: 112
Reputation: 216343
Simply setting the DropDownStyle property to DropDownList will force the user to select only from the items already present in the combobox and typing the letters will select the corresponding item. So you can remove any code written to force an automatic selection
Do not forget to set also AutoCompleteMode to Suggest or SuggestAppend.
Upvotes: 2