I want to substitute the names, pulling them up from the base

private void textBox3_TextChanged_1(object sender, EventArgs e) { TextBox t = sender as TextBox; if (t != null) { //say you want to do a search when user types 3 or more chars if (t.Text.Length >= 3) { var results = JsonConvert.DeserializeObject<dynamic>(txb); var source = new AutoCompleteStringCollection(); string name = null; foreach (var element in results) { name = (string)element.name; source.Add(name); } textBox3.AutoCompleteCustomSource = source; 

thus the value is immediately substituted instead of several.

    1 answer 1

    You each time you change the textBox parse json. Do not you think that it is expensive? Parse it once and remember (create a separate field).

    Further, on the substitution problem. Most likely, the mod SuggestAppend . And he, in conjunction with the dynamic change AutoCompleteCustomSource textBox'a due to its mechanism gives just such a problem. If you don’t need to add the word in the textbox itself, and only need a list, you can change the mod to Suggest . If this behavior does not fit, then in your case, AutoCompleteCustomSource can be changed only once: when the textBox text has become more than 3 characters. You can try the following method:

     public Form1() { InitializeComponent(); textBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox3.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox3.AutoCompleteCustomSource = EmptyAutoCompleteCustomSource; } private AutoCompleteStringCollection EmptyAutoCompleteCustomSource { get; set; } = new AutoCompleteStringCollection(); private AutoCompleteStringCollection AutoCompleteCustomSource { get; set; } private AutoCompleteStringCollection CreateAutoCompleteCustomSource() { AutoCompleteStringCollection source = new AutoCompleteStringCollection(); var results = JsonConvert.DeserializeObject<dynamic>(txb); foreach(var element in results) source.Add((string)element.name); return source; } private void UpdateAutoCompleteCustomSource() => AutoCompleteCustomSource = CreateAutoCompleteCustomSource(); private void EnsureAutoCompleteCustomSource() { if(AutoCompleteCustomSource == null) UpdateAutoCompleteCustomSource(); } private void textBox3_TextChanged(object sender, EventArgs e) { TextBox t = sender as TextBox; if(t != null) { // Проверки на textBox3.AutoCompleteCustomSource нужны, чтобы изменение AutoCompleteCustomSource // было один раз при выполнении данного условия if(t.Text.Length >= 3 && textBox3.AutoCompleteCustomSource == EmptyAutoCompleteCustomSource) { EnsureAutoCompleteCustomSource(); textBox3.AutoCompleteCustomSource = AutoCompleteCustomSource; } else if(t.Text.Length < 3 && textBox3.AutoCompleteCustomSource == AutoCompleteCustomSource) textBox3.AutoCompleteCustomSource = EmptyAutoCompleteCustomSource; } } 
    • getting a list of 500 values ​​right away is not very good. How to be that the list was updated after entering 3 letters on like? - des1roer
    • @ des1roer In your example, the list of 500 elements stretched every time the textbox was changed, which is not good. And 500 elements is not so much. - skubarenko
    • @ des1roer if 500 elements is still a lot for you, and every time you access the file system / database is the norm, then you need to change the AutoCompleteCustomSource after a short delay when the AutoComplete mechanism works. You can do this through a timer. If the mod is just Suggest, then you can do without delays and immediately change. - skubarenko
    • but he pulls by the condition like '3 elem' - des1roer
    • @ des1roer, but 500 elements stretch. Unless, of course, the production code is the same as in the example. You do it: entered 2 characters - nothing happened, entered 3 characters - uploaded 500 records; they entered 4 characters - they loaded the same 500 records, 5 characters - again the same 500 records, etc. I have: once downloaded at the very beginning. I can redo it a bit later so that the appeal to my AutoCompleteCustomSource property is lazy, i.e. loading of records will be once and when the first time entered characters will be more than 3. I will be from the computer, I will edit the answer. - skubarenko