AutoComplete auto-messaging that does NOT allow custom text

C # WinForms: I used Combobox with these properties: DropDownStyle: DropDown AutoCompleteSource: ListItems AutoCompleteMode: SuggestAppend

so now when i type in combobox it offers items from a list of its items. Good. But the problem is that I do not want to be able to enter everything that I want, I just want to be able to enter from the valid elements that are in his list. How can I fix this part?

Thanks.

+6
source share
5 answers

You will need to populate the Items list with your values ​​(manually or via data binding), and then set DropDownStyle to DropDownList .

The combobox will not look like a text box, but if it has focus, entering it will automatically select the best match from the Items list.

(This is the recommended way to install Combobox so as not to allow custom text.)

Alternatively, if you want the style to be DropDown, grab the KeyPress event of the control and do a quick check of the control text plus e.KeyChar , and if it is not found in the list, set e.Handled = True . This will block all keystrokes that result in something not listed.

+5
source

This corresponds to the lines in the combo box.

 int index = combobox1.FindString(combobox1.Text); if (index < 0) { MessageBox.Show("Invalid Record"); combobox1.Focus(); return; } 
+1
source

The list control does not support this directly.

You will need to write handlers to verify that the item from the list has been entered, and ask the user if not.

0
source

This is not quite like automatic completion, but if you set DropDownStyle to DropDownList, it will only contain entries that are in the Items collection. However, the default behavior of this mode is that each letter you enter goes to the first match starting with that letter. So, if you want them to continue typing extra characters after the first letter, you can set AutoCompleteSource to ListItems and then set AutoCompleteMode to add.

0
source

Another option may be registered in the TextChanged or TextUpdated , and if the text already entered does not meet your conditions, change it accordingly.

0
source

Source: https://habr.com/ru/post/915996/


All Articles