try
[DefaultProperty("AutoCompleteEnabled"),
DefaultEvent("SelectedIndexChanged")]
public class AutoCompleteComboBox : ComboBox
{
///
/// Event raised if a match isn't found and
/// is set to true
///
public event CancelEventHandler MatchNotFound;
///
/// Internal value to track if we should bother auto completing or not
///
private bool _AutoCompleteEnabled = true;
///
/// Internal value to track if we should limit to list or not.
///
private bool _LimitToList = true;
///
/// Enable or disable autocomplete
///
[system.ComponentModel.Category("Behavior"),
System.ComponentModel.Description("Enables or disables the autocompletion functionality")]
public virtual bool AutoCompleteEnabled
{
get {return _AutoCompleteEnabled;}
set {_AutoCompleteEnabled = value;}
}
protected override void OnValidating(CancelEventArgs e)
{
if (LimitToList)
{
int i = this.FindStringExact(Text);
if (i == -1)
{
OnNotInList(e);
}
else
{
SelectedIndex = i;
}
}
base.OnValidating(e);
}
[Category("Behavior"),
System.ComponentModel.Description("Limits the selection to items in the list.")]
public bool LimitToList
{
get { return _LimitToList; }
set { _LimitToList = value; }
}
protected virtual void OnNotInList(CancelEventArgs e)
{
if (MatchNotFound != null)
{
MatchNotFound(this, e);
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
string currentText = Text;
int i;
if (e.KeyChar == Convert.ToChar(Keys.Back) )
{
if(Text.Length == 0)
{return;} //Don't backspace when nothing there
Text = Text.Substring(0, SelectionStart - 1);
currentText = Text;
SelectionStart=Text.Length;
}
i = FindString(currentText);
if( i >= 0 )
{
SelectedIndex = i;
Select (currentText.Length, Text.Length);
}
base.OnKeyPress (e);
}
}