How can I dynamically change auto complete entries in a C# combobox or textbox?

I think you might want to get out the reflector and look at overriding the autocomplete behaviour in the combobox itself. I'm certain the autocompletion would call a function which accesses the autocomplete list. If you can find this function and override it, you can use any behaviour you want.

I think you might want to get out the reflector and look at overriding the autocomplete behaviour in the combobox itself. I'm certain the autocompletion would call a function which accesses the autocomplete list. If you can find this function and override it, you can use any behaviour you want.

See what documentation you can find on the combobox class itself.

I haven't test this, but it may be worth a shot. Instead of clearing the AutoCompleteCustomSource, double buffer by keeping two instances. When the text changes, call GetNameSuggestions() and build the strings for the one that's not currently being used, then set ComboName.

AutoCompleteCustomSource to the one you just set up. I think it should look something like this. AutoCompleteCustomSource accs_a; AutoCompleteCustomSource accs_b; bool accs_check = true; //true for accs_a, false for accs_b void InitializeComboBox() { ComboName.

AutoCompleteMode = AutoCompleteMode. SuggestAppend; ComboName. AutoCompleteSource = AutoCompleteSource.

CustomSource; accs_a = new AutoCompleteStringCollection(); accs_b = new AutoCompleteStringCollection(); ComboName. AutoCompleteCustomSource = accs_a; ComboName. TextChanged += new EventHandler( ComboName_TextChanged ); } void ComboName_TextChanged( object sender, EventArgs e ) { string text = this.ComboName.

Text; if(accs_check) { accs_b.Clear(); accs_b. AddRange(GetNameSuggestions( text )); accs_check = false; } else { accs_a.Clear(); accs_a. AddRange(GetNameSuggestions( text )); accs_check = true; } this.ComboQuery.

AutoCompleteCustomSource = accs_check? Accs_a : accs_b; }.

Update: main reason to put the lock on this place is its working :) most of "mysterious exception" that I ever have, after this trick disappear the lock like in this code, can help with your exception as you mention before, there is less problem with using textbox in this code, SuggestAppend working fine private void Form1_Load(object sender, EventArgs e) { textBox1. AutoCompleteMode = AutoCompleteMode. SuggestAppend; textBox1.

AutoCompleteSource = AutoCompleteSource. CustomSource; textBox1. TextChanged+=new EventHandler(textBox1_TextChanged); col1.

AddRange(new string { "avi avi", "avram avram" }); col2. AddRange(new string { "boria boria", "boris boris" }); textBox1. AutoCompleteCustomSource = col1; textBox1.

AutoCompleteMode = AutoCompleteMode. SuggestAppend; } AutoCompleteStringCollection col1 = new AutoCompleteStringCollection(); AutoCompleteStringCollection col2 = new AutoCompleteStringCollection(); object locker = new object(); private void textBox1_TextChanged(object sender, EventArgs e) { lock (locker) { if (textBox1.Text. StartsWith("a") && textBox1.

AutoCompleteCustomSource! = col1) { textBox1. AutoCompleteCustomSource = col1; } if (textBox1.Text.

StartsWith("b") && textBox1. AutoCompleteCustomSource! = col2) { textBox1.

AutoCompleteCustomSource = col2; } } }.

I haven't tried that exact code, but I think it is basically the same as replacing the existing AutoCompleteCustomSource with a new AutoCompleteStringCollection each time, which I have tried and which throws occasional exceptions. – Sam Hopkins Feb 8 '09 at 2:15 That lock wouldn't prevent the exception because the TextChanged handler isn't re-entrant anyway - I've checked this. – Sam Hopkins Feb 8 '09 at 9:40 Also, I've tried something similar (only occasionally changing the AutoCompleteStringCollection, instead of on every character), and I still get exceptions under certain circumstances.

There may be a race condition in the underlying code, so I'm not comfortable using this in production environments – Sam Hopkins Feb 8 '09 at 9:44.

I am running into the same situation. Clear() seems to cause the exception. I removed the call to clear and I'm getting the correct suggestions event though the collection keeps on growing... Also, regarding the private members: you can access them using reflection: PropertyInfo props = object.GetType().

GetProperties({flags go here}); props0. SetValue(this, new object { 0 }).

I came here initially looking for a solution, but have now found my own. The trick is not to call Clear() on the AutoCompleteCustomSource but to remove all items in a for loop and then rebuild the list with the new data. In my case (a book collection application) I'm retrieving author names from a database with a specific starting letter, instead of the whole lot.

Note that this will only work if the textbox part of the combobox is or has become empty. Private void cboAuthor_KeyDown(object sender, KeyEventArgs e) { if (cboAuthor.Text. Length == 0) { // Next two lines simple load data from the database in the // into a collection (var gateway), base on first letter in // the combobox.

This is specific to my app. Var gateway = new AuthorTableGateway(); gateway. LoadByFirstLetter(Char.

ConvertFromUtf32(e. KeyValue)0); // Clear current source without calling Clear() for (int I = 0; I RemoveAt(0); // Rebuild with new data foreach (var author in gateway) authorsAutoComplete. Add(author.

AuthorName); } }.

Further testing proved this solution to show erratic behaviour, even after correcting the erroneous "clear" loop which should have been: while (authorsAutoComplete. Count > 0) authorsAutoComplete. RemoveAt(0); Sadly, I have no other solution.

– Jim Cramer Jun 18 '10 at 22:08.

For me the secret was using the TextChanged event and none of the KeyDown/Up/Press, etc. Update: After having other issues with dynamically changing the AutoCompleteCustomSource I eventually abandoned using the builtin Autocomplete functionality and implemented my own in much shorter time than I had wasted on it originally. There seems to be some issues in unmanaged code that implements the ComboBox control. Specifically, I was having issues with the TextChanged event handler firing when it should.

I decided to only use the OnKeyDown/Press/Up handlers in my custom implementation and that seemed to be more reliable.

I have used TextChange Event for my autocompletemode. But how to search sub-string in it. Eg.

If I type "Thom" it should suggest "Thomas", "Dthomas" as well as "Dr. Thomas". Any help – rapsalands Sep 28 at 1:31.

I had the same problem, and found an extremely simple workaround. As everybody else here, I couldn't find any means to control de behaviour of the component, so I had to accept it. The natural behaviour is: you can't dynamically populate the list every time the user types into the text box.

You have to populate it once, and then the AutoComplete mechanism takes control. The conclusion is: you should populate the AutoCompleteCustomSource with every possible entry in you database to make it work as we want. Of course this is not viable if you have millions of records to populate the list.

Performance issues in data transfer and the AutoComplete mechanism itself will not allow you to do that. The compromise solution I found was: dynamically populate the AutoCompleteCustomSource every time that the Text length reaches exactly N chars (3 in my case). This worked because complexity was drastically reduced.

The number of records that are fetched from the database that match these 3 initial chars was small enough to avoid any performance issues. The major drawback is: users will not be presented the AutoComplete list until they type the N-th char. But it seems like users don't really expect a meaningful AutoComplete list before 3 chars are typed.

Hope this helps.

Haven't try this, but for your specific case you could code something like: private void txtAutoComplete_KeyUp(object sender, KeyEventArgs e) { String text = txtAutoComplete. Text; if (text. EndsWith(" ")) { string suggestions = GetNameSuggestions( text ); //put text + " " at the begin of each array element txtAutoComplete.

AutoCompleteCustomSource.Clear(); txtAutoComplete. AutoCompleteCustomSource. AddRange( suggestions ); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions