/// <summary>
/// ComboBox 的摘要说明。
/// </summary>
public class ComboBox : System.Windows.Forms.ComboBox
{
#region 字段
/// <summary>
/// 表示最近一次的用户交互操作是否是键盘操作。
/// </summary>
private bool isKeyPressed;
/// <summary>
/// 表示是否启用自动完成功能。
/// </summary>
private bool autoComplete;
#endregion
#region 属性
/// <summary>
/// 获取是否启用自动完成功能。
/// </summary>
[Bindable(true),Browsable(true),SRCategory(SR.CatBehavior),SRDescription(SR.ComboBoxAutoCompleteDescr)]
public bool AutoComplete
{
get
{
return autoComplete;
}
set
{
autoComplete = value;
}
}
#endregion
#region 构造函数
/// <summary>
///
/// </summary>
public ComboBox()
{
isKeyPressed = false;
autoComplete = false;
}
#endregion
#region 保护方法
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
bool flag = isKeyPressed;
isKeyPressed = false;
if(DropDownStyle != ComboBoxStyle.DropDown)
{
base.OnTextChanged (e);
return;
}
if(!flag)
{
base.OnTextChanged (e);
return;
}
if(!autoComplete)
{
base.OnTextChanged (e);
return;
}
BeginUpdate();
string newText = Text;
if(newText!="")
{
if(FindStringExact(newText)!=-1)
{
if(DroppedDown)
{
DroppedDown = false;
}
}
else if(FindString(newText)!=-1)
{
DroppedDown = true;
Text = newText;
Select(newText.Length,0);
}
}
else if(DroppedDown)
{
DroppedDown = false;
}
EndUpdate();
base.OnTextChanged (e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
isKeyPressed = true;
base.OnKeyPress (e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
isKeyPressed = false;
base.OnMouseDown (e);
}
#endregion
}