ComboBox AutoComplete with key / value
I have a ComboBox with the following code:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
using (var service = WebServiceHelper.GetCoreService())
{
string physicianXml = service.SearchPhysicians(SessionInfo.Current.ClientCode, SessionInfo.Current.MachineName,
SessionInfo.Current.Username, comboBox1.Text);
var physicians = PhysicianItemList.FromXml(physicianXml);
AutoCompleteStringCollection autoCompleteStringCollection = new AutoCompleteStringCollection();
foreach (var physician in physicians.Items)
{
autoCompleteStringCollection.Add(physician.LastName + ", " + physician.FirstName);
}
comboBox1.AutoCompleteCustomSource = autoCompleteStringCollection;
comboBox1.Select(comboBox1.Text.Length, 0);
}
}
Basically, the user enters the first few characters of the doctor's name, and he should populate the autocomplete list with top 100 matching entries. It works fine, but I need to associate it with a key (either the PC from the table or the NPI number of the doctor). It seems that AutoCompleteStringCollection
does not support the keys. Can anyone suggest a way to do this? There are about 7 million records in the table, so I don't want to pre-fill the ComboBox.
thank
source to share
When you create yours AutoCompleteStringCollection
, create a Dictionary<String, int>
for the name, id pairs as well. Then use some event (text field validation or user request / click save) to find and set the id. You can save the dictionary in a text box Tag
.
Edit
For some reason I thought you were working with a textbox control. Forget about AutoCompleteStringCollection
and just create Dictionary<String, int>
. For the combobox to set your autocompletesource to ListItems, set the display name and value of the displayed list and set the data source in the dictionary.
combobox.DisplayMember = "key";
combobox.ValueMember = "value";
combobox.AutocompleteSource = AutocompleteSource.ListItems;
combobox.DataSource = myDictionary;
However, you only have to fill the datasource and autocomplete once when the user enters n characters into the combo box, otherwise it becomes an error. I tried to use this for dynamic autocomplete once (for example the list is cleared if the user clears the text and types), but I had to forget about the dropdown and use a combo box approach like this user
Too late, but maybe someone will use this code:
this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
RNProveedor rnProveedor = new RNProveedor();
var listaProveedores = rnProveedor.Buscar();
Dictionary<int, String> dicTemp = new Dictionary<int, string>();
foreach (var entidad in listaProveedores)
{
dicTemp.Add(entidad.ProvNro, entidad.ProNombre);
}
this.DataSource = new BindingSource(dicTemp, null);
this.DisplayMember = "Value";
this.ValueMember = "Key";
And to choose a value
public int GetValorDecimal()
{
KeyValuePair<int, string> objeto = (KeyValuePair<int, string>)this.SelectedItem;
return objeto.Key;
}
For this example, you will have no problem with duplicate lines as the examples above
source to share