using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace AutoCompleteComboBox
{
/// <summary>
/// 自動匹配下拉框
/// </summary>
public class AutoCompleteComboBox : System.Windows.Forms.ComboBox
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public AutoCompleteComboBox()
{
// 該調用是 Windows.Forms 窗體設計器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 調用後添加任何初始化
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 組件設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.KeyDown +=new KeyEventHandler(AutoCompleteComboBox_KeyDown);
}
#endregion
private void AutoCompleteComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string showword = this.Text.Trim();
foreach (string item in this.Items)
{
for(int i = 0; i < item.Length; i++)
{
if (item.Substring(0,i) == this.Text.Trim())
{
this.SelectedItem = item;
break;
}
}
for(int i = 1; i <= item.Length - item.IndexOf("-")-1; i++)
{
if (item.Substring(item.IndexOf("-")+1,i) == this.Text.Trim())
{
this.SelectedItem = item;
break;
}
}
if (this.Text.Trim() != showword)
{
break;
}
}
}
}
}
}