AutoComplete ComboBox

xiaoxiao2021-03-06  65

I have encountered someone on the Internet to ask questions about the automatic filling of ComboBox. Of course, I didn't care, who I learned later because software development needs to encounter the same problem. So I have to find information everywhere, who knows the only functionality that can only be automatically filled, and does not touch the same information like IE column, which can expand ComboBox and highlight the content. (Accent, there is 3 days of record under ComboBox, respectively: eqweq, fasdfs, tyr "When I enter the character e, I should display EQWEQ in the text box and expand the pull box, select EQWEQ). Warm, I have to do this After study, after a slight change, the function I expected, the source code is as follows:

using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace DigitalCult {///

/// Summary description for . ACComboBox /// public class ACComboBox: System.Windows.Forms.ComboBox {private bool autoComplete; [DefaultValue (true), Description ( "Auto-completes text if a match is found in the items collection.") , Category ( "Behavior")] public bool AutoComplete {get {return autoComplete;} set {autoComplete = value;}} /// /// Required designer variable /// private Container components =. Null; /// /// default constructor. /// public acid accombox () {// this call is required by the windows.forms forment (); // address (); // add anyinitializ ation after the InitComponent call this.autoComplete = true; this.KeyPress = new KeyPressEventHandler (this.OnKeyPress);} /// /// Clean up any resources being used /// protected override. void Dispose (bool disposing) {if (disposing) {if (components = null!) components.Dispose ();} base.Dispose (disposing);} private void OnKeyPress (object sender, KeyPressEventArgs e) {if (autoComplete) { Accombox acidboBOX =

(ACCOMBOBOX) Sender; if (! 8)) {searchitems (AccomboBox, Ref e);} else e.handled = false;} else e.handled = false;} /// < Summary> /// Searches the Commary> /// Searches the Commary> /////// ///// Private void SearchItems (aCComboBox acComboBox, ref KeyPressEventArgs e) {int selectionStart = acComboBox.SelectionStart; int selectionLength = acComboBox.SelectionLength; int selectionEnd = selectionStart selectionLength; int index; StringBuilder sb = new StringBuilder (); sb.Append (acComboBox.Text. Substring (0, selectStart) .append (E.keychar.tostring ()) .append (accomboBox.tex t.Substring (selectionEnd)); index = acComboBox.FindString (sb.ToString ()); if (index == -1) e.Handled = false; else {acComboBox.SelectedIndex = index; acComboBox.Select (selectionStart 1 AccomboBox.Text.Length- (SelectionStart 1)); E.Handled = true;}} #Region Component Designer generated code ///

/// Required method for designer support - do not modifi ////// Contents of this method with the code editor. ///

转载请注明原文地址:https://www.9cbs.com/read-111430.html

New Post(0)