combobox and auto-completion
-
Hello, i'd like to use a combobox with auto-completion like the one used by internet explorer to browse. How can i do that? Of course i'd like to add any items inside, like "Boat, "Car", "Moto", etc.... Thanks a lot
-
Hello, i'd like to use a combobox with auto-completion like the one used by internet explorer to browse. How can i do that? Of course i'd like to add any items inside, like "Boat, "Car", "Moto", etc.... Thanks a lot
-
Hello, i'd like to use a combobox with auto-completion like the one used by internet explorer to browse. How can i do that? Of course i'd like to add any items inside, like "Boat, "Car", "Moto", etc.... Thanks a lot
Sample c#
//************************************************************************************ // Call the corresponding functions from your form's Combobox's KeyUp and // Leave events //************************************************************************************ private void ACCombo_KeyUp(ComboBox cbo, KeyEventArgs e) { //Allow select keys without autocompleting switch(e.KeyCode) { case Keys.Back: case Keys.Left: case Keys.Right: case Keys.Up: case Keys.Delete: case Keys.Down: return; } //Get the typed text and find it on the list string strTypedText = cbo.Text; int intFoundIdx = cbo.FindString(strTypedText); //If we found the typed text in the list then autocomplete if(intFoundIdx >= 0) { //Get the item from the list (Return type depends if datasource was bound //or list created) object objFoundItem = cbo.Items[intFoundIdx]; //Use the listcontrolgetitemtext to resolve the name in case the combo //was data bound string strFoundText = cbo.GetItemText(objFoundItem); //Append then found text to the typed text to preserve case string strAppendText = strFoundText.Substring(strTypedText.Length); cbo.Text = strTypedText + strAppendText; //Select the appended text cbo.SelectionStart = strTypedText.Length; cbo.SelectionLength = strAppendText.Length; } } private void ACCombo_Leave(ComboBox cbo) { int intFoundIdx = cbo.FindStringExact(cbo.Text); cbo.SelectedIndex = intFoundIdx; }
Let me know if you need this for VB.NET:) I reject to reality and subsitute my own! - Adam Savage, Mythbuster life is like a roll of toilet paper. The closer it gets to the end, the faster it goes. -
Sample c#
//************************************************************************************ // Call the corresponding functions from your form's Combobox's KeyUp and // Leave events //************************************************************************************ private void ACCombo_KeyUp(ComboBox cbo, KeyEventArgs e) { //Allow select keys without autocompleting switch(e.KeyCode) { case Keys.Back: case Keys.Left: case Keys.Right: case Keys.Up: case Keys.Delete: case Keys.Down: return; } //Get the typed text and find it on the list string strTypedText = cbo.Text; int intFoundIdx = cbo.FindString(strTypedText); //If we found the typed text in the list then autocomplete if(intFoundIdx >= 0) { //Get the item from the list (Return type depends if datasource was bound //or list created) object objFoundItem = cbo.Items[intFoundIdx]; //Use the listcontrolgetitemtext to resolve the name in case the combo //was data bound string strFoundText = cbo.GetItemText(objFoundItem); //Append then found text to the typed text to preserve case string strAppendText = strFoundText.Substring(strTypedText.Length); cbo.Text = strTypedText + strAppendText; //Select the appended text cbo.SelectionStart = strTypedText.Length; cbo.SelectionLength = strAppendText.Length; } } private void ACCombo_Leave(ComboBox cbo) { int intFoundIdx = cbo.FindStringExact(cbo.Text); cbo.SelectedIndex = intFoundIdx; }
Let me know if you need this for VB.NET:) I reject to reality and subsitute my own! - Adam Savage, Mythbuster life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.