C# Autocomplte combobox
public partial class VComboBox : ComboBox
{
public event System.ComponentModel.CancelEventHandler NotInList;
private bool _limitToList = true;
private bool _inEditMode = false;
public VComboBox() : base()
{
InitializeComponent();
this.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FormattingEnabled = true;
this.Location = new System.Drawing.Point(118, 329);
this.MaxDropDownItems = 10;
this.Name = "comboBox1";
this.Size = new System.Drawing.Size(197, 26);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
[Category("Behavior")]
public bool LimitToList
{
get { return _limitToList; }
set { _limitToList = value; }
}
protected virtual void
OnNotInList(System.ComponentModel.CancelEventArgs e)
{
if (NotInList != null)
{
NotInList(this, e);
}
}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.DroppedDown = true;
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
this.DroppedDown = false;
}
protected override void OnTextChanged(System.EventArgs e)
{
if (_inEditMode)
{
string input = Text;
int index = FindString(input);
if (index >= 0)
{
_inEditMode = false;
SelectedIndex = index;
_inEditMode = true;
Select(input.Length, Text.Length);
this.DroppedDown = true;
}
}
base.OnTextChanged(e);
}
protected override void
OnValidating(System.ComponentModel.CancelEventArgs e)
{
if (this.LimitToList)
{
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)
{
_inEditMode =
(e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
base.OnKeyDown(e);
}