Csharp/C Sharp/Components/ComboBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Auto Complete ComboBox

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private AutoCompleteComboBox combo; 
   public Form1()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     combo = new AutoCompleteComboBox();
     combo.Location = new System.Drawing.Point(64, 32);
     combo.Size = new System.Drawing.Size(150, 20);
           combo.Items.Add("Aaaaaa");
           combo.Items.Add("Bbbbbbbbb");
           combo.Items.Add("Ccccccccccc");
     
     Controls.Add(combo);
     CenterToScreen();
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
 }
   
   public class AutoCompleteComboBox : System.Windows.Forms.ruboBox
   {
       public event System.ruponentModel.CancelEventHandler NotInList;
       private bool strict = true;
       private bool isEditing = false;
       public AutoCompleteComboBox() : base() {
       }
       public bool Strict
       {
           get { return strict; }
           set { strict = value; }
       }
       protected virtual void OnNotInList(System.ruponentModel.CancelEventArgs e)
       {
           if (NotInList != null)
           {
               NotInList(this, e);
           }
       }   
       protected override void OnTextChanged(System.EventArgs e)
       {
           if (isEditing)
           {
               string input = Text;
               int index = this.FindString(input);
               if (index >= 0)
               {
                   isEditing = false;
                   SelectedIndex = index;
                   isEditing = true;
                   Select(input.Length, Text.Length);
               }
           }
           base.OnTextChanged(e);
       }
       protected override void OnValidating(System.ruponentModel.CancelEventArgs e)
       {
           if (this.Strict)
           {
               int pos = this.FindStringExact(this.Text);
       
               if (pos == -1)
               {
                   OnNotInList(e);
               }
               else
               {
                   this.SelectedIndex = pos;
               }
           }
           base.OnValidating(e);
       }
       protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
       {
           isEditing = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
           base.OnKeyDown(e);
       }
   }
          
      </source>


Buildin AutoCompleteMode for ComboBox

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

   private System.Windows.Forms.ruboBox lstColors;
 public Form1() {
       InitializeComponent();
       string[] colorNames;
       colorNames = System.Enum.GetNames(typeof(KnownColor));
       lstColors.Items.AddRange(colorNames);
 }
   private void InitializeComponent()
   {
       this.lstColors = new System.Windows.Forms.ruboBox();
       this.SuspendLayout();
       this.lstColors.AutoCompleteMode = ((System.Windows.Forms.AutoCompleteMode)((System.Windows.Forms.AutoCompleteMode.Suggest | System.Windows.Forms.AutoCompleteMode.Append)));
       this.lstColors.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
       this.lstColors.FormattingEnabled = true;
       this.lstColors.Location = new System.Drawing.Point(13, 13);
       this.lstColors.Name = "lstColors";
       this.lstColors.Size = new System.Drawing.Size(267, 21);
       this.lstColors.TabIndex = 0;
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
       this.ClientSize = new System.Drawing.Size(296, 82);
       this.Controls.Add(this.lstColors);
       this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
       this.Name = "Form1";
       this.Text = "AutoComplete";
       this.ResumeLayout(false);
   }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


Use an Autocomplete Combo Box

<source lang="csharp"> using System; using System.Windows.Forms; using System.Drawing; public class AutoCompleteComboBoxTest : System.Windows.Forms.Form {

  public AutoCompleteComboBoxTest(){
       AutoCompleteComboBox combo = new AutoCompleteComboBox();
       combo.Location = new Point(10,10);
       this.Controls.Add(combo);
       combo.Items.Add("Aaaaaa");
       combo.Items.Add("Bbbbbbbbb");
       combo.Items.Add("Ccccccccccc");
  }
  public static void Main(){
      Application.Run(new AutoCompleteComboBoxTest());
  } 

}

public class AutoCompleteComboBox : ComboBox {

   private bool controlKey = false;
   protected override void OnKeyPress( System.Windows.Forms.KeyPressEventArgs e) {
       base.OnKeyPress(e);
       if (e.KeyChar == (int)Keys.Escape) {
           this.SelectedIndex = -1;
           this.Text = "";
           controlKey = true;
       } else if (Char.IsControl(e.KeyChar)) {
           controlKey = true;
       } else {
           controlKey = false;
       }
   }
   protected override void OnTextChanged(System.EventArgs e) {
       base.OnTextChanged(e);
       if (this.Text != "" && !controlKey) {
           string matchText = this.Text;
           int match = this.FindString(matchText);
           if (match != -1) {
               this.SelectedIndex = match;
               this.SelectionStart = matchText.Length;
               this.SelectionLength = this.Text.Length - this.SelectionStart;
           }
       }
   }

}

      </source>