简体   繁体   中英

Combo Box Select

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM