There are many different variations to the autocomplete control. Sometimes, the control fills in values based on a list of recent selections (as Microsoft Excel does when entering cell values) or might display a drop-down list of near matches (as Microsoft Internet Explorer does when typing a URL). You can create a basic autocomplete combo box by handling the KeyPress and TextChanged events, or by creating a custom class that derives from ComboBox and overrides the OnKeyPress and OnTextChanged methods. This recipe takes the latter approach.
In the OnKeyPress method, the combo box determines whether or not an autocomplete replacement should be made. If the user pressed a character key (such as a letter) the replacement can be made, but if the user has pressed a control key (such as the backspace key, the cursor keys, and so on), no action should be taken. The OnTextChanged method performs the actual replacement after the key processing is complete. This method looks up the first match for the current text in the list of items, and then adds the rest of the matching text. After the text is added, the combo box selects the characters between the current insertion point and the end of the text. This allows the user to continue typing and replace the autocomplete text if it isn't what the user wants.
Here's the full code for the AutoCompleteComboBox class:
using System; using System.Windows.Forms; public class AutoCompleteComboBox : ComboBox { // Track if a special key is pressed // (in which case the text replacement operation will be skipped). private bool controlKey = false; // Determine whether a special key was pressed. protected override void OnKeyPress( System.Windows.Forms.KeyPressEventArgs e) { base.OnKeyPress(e); if (e.KeyChar == (int)Keys.Escape) { // Clear the text. this.SelectedIndex = -1; this.Text = ""; controlKey = true; } else if (Char.IsControl(e.KeyChar)) { controlKey = true; } else { controlKey = false; } } // Perform the text substitution. protected override void OnTextChanged(System.EventArgs e) { base.OnTextChanged(e); if (this.Text != "" && !controlKey) { // Search for a matching entry. string matchText = this.Text; int match = this.FindString(matchText); // If a matching entry is found, insert it now. if (match != -1) { this.SelectedIndex = match; // Select the added text so it can be replaced // if the user keeps typing. this.SelectionStart = matchText.Length; this.SelectionLength = this.Text.Length - this.SelectionStart; } } } }
To test the AutoCompleteComboBox, you can create a simple Windows client that adds the combo box to a form and fills it with a list of words. In this example, the list of words is retrieved from a text file and the control is added manually. You could also compile the AutoCompleteComboBox class to a separate class library assembly and then add it to the Toolbox, so you could add it to a form at design time.
using System; using System.Windows.Forms; using System.Drawing; using System.IO; public class AutoCompleteComboBoxTest : System.Windows.Forms.Form { // (Designer code omitted.) private void AutoCompleteComboBox_Load(object sender, System.EventArgs e) { // Add the combo box to the form. AutoCompleteComboBox combo = new AutoCompleteComboBox(); combo.Location = new Point(10,10); this.Controls.Add(combo); // Add the word list. FileStream fs = new FileStream("words.txt", FileMode.Open); using (StreamReader r = new StreamReader(fs)) { while (r.Peek() > -1) { string word = r.ReadLine(); combo.Items.Add(word); } } } }
try this...C# Autocomplete combobox
ReplyDeleteLing