Auto Complete ComboBox : ComboBox « Components « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Components » ComboBoxScreenshots 
Auto Complete ComboBox
Auto Complete ComboBox


  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  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(513);
      this.ClientSize = new System.Drawing.Size(292273);

      combo = new AutoCompleteComboBox();
      combo.Location = new System.Drawing.Point(6432);
      combo.Size = new System.Drawing.Size(15020);

            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.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);
        }
    }

           
       
Related examples in the same category
1.Buildin AutoCompleteMode for ComboBoxBuildin AutoCompleteMode for ComboBox
2.Use an Autocomplete Combo BoxUse an Autocomplete Combo Box
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.