private void SearchValues(LegalValuesComboBox legalValuesComboBox, ref KeyPressEventArgs e) { // Code below adapted from Raj Cool's AutoComplete combo box tutorial int selectionStart = legalValuesComboBox.SelectionStart; int selectionLength = legalValuesComboBox.SelectionLength; int selectionEnd = selectionStart + selectionLength; int index; StringBuilder sb = new StringBuilder(); // Create a new word with the user's last input key as the last char in the word sb.Append(legalValuesComboBox.Text.Substring(0, selectionStart)).Append(e.KeyChar.ToString()).Append(legalValuesComboBox.Text.Substring(selectionEnd)); // Get the first item in the string that starts with our word, created in the string builder above index = legalValuesComboBox.FindString(sb.ToString()); // if the index isn't found... if (index == -1) { e.Handled = false; } else { legalValuesComboBox.SelectedIndex = index; legalValuesComboBox.Select(selectionStart + 1, legalValuesComboBox.Text.Length - (selectionStart + 1)); e.Handled = true; } }
/// <summary> /// Handles the KeyPress event /// </summary> /// <param name="sender">Object that fired the event</param> /// <param name="e">.NET supplied key event parameters</param> private void OnKeyPress(object sender, KeyPressEventArgs e) { LegalValuesComboBox legalValuesComboBox = sender as LegalValuesComboBox; if (!e.KeyChar.Equals('\b')) { SearchValues(legalValuesComboBox, ref e); } else { e.Handled = false; } }
private void SearchValues(LegalValuesComboBox legalValuesComboBox, ref KeyPressEventArgs e) { // Code below adapted from Raj Cool's AutoComplete combo box tutorial int selectionStart = legalValuesComboBox.SelectionStart; int selectionLength = legalValuesComboBox.SelectionLength; int selectionEnd = selectionStart + selectionLength; int index; StringBuilder sb = new StringBuilder(); // Create a new word with the user's last input key as the last char in the word sb.Append(legalValuesComboBox.Text.Substring(0, selectionStart)).Append(e.KeyChar.ToString()).Append(legalValuesComboBox.Text.Substring(selectionEnd)); // Get the first item in the string that starts with our word, created in the string builder above index = legalValuesComboBox.FindString(sb.ToString()); // if the index isn't found... if (index == -1) { e.Handled = false; } else { legalValuesComboBox.SelectedIndex = index; legalValuesComboBox.Select(selectionStart + 1, legalValuesComboBox.Text.Length - (selectionStart + 1)); e.Handled = true; } }