Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple de AutoComplet avec ComboBox

Créer,une,zone,texte,numérique
{filelink=15667}

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
 
  public class AutoCompletComboBox : System.Windows.Forms.Form
  {
    private AutoCompleteComboBox cmbMois; 
 
    public AutoCompletComboBox()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(450, 90);
      this.Text = "AutoComplet ComboBox";
      cmbMois = new AutoCompleteComboBox();
      cmbMois.Location = new System.Drawing.Point(64, 32);
      cmbMois.Size = new System.Drawing.Size(150, 20);
      cmbMois.Items.Add("Janvier");
      cmbMois.Items.Add("Fevrier");
      cmbMois.Items.Add("Mars");
      cmbMois.Items.Add("Avril");
 
      Controls.Add(cmbMois);
      CenterToScreen();
    }
 
    static void Main()
    {
      Application.Run(new AutoCompletComboBox());
    }
  }
 
    public class AutoCompleteComboBox : System.Windows.Forms.ComboBox
    {
        public event System.ComponentModel.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.ComponentModel.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.ComponentModel.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);
        }
    }

No comments yet.

Leave a Comment