在Web的應用方面有js的插件實現自動完成(或叫智能提示)功能,但在WinForm窗體應用方面就沒那麼好了。
TextBox控件本身是提供了一個自動提示功能,只要用上這三個屬性:
AutoCompleteCustomSource:AutoCompleteSource 屬性設置為CustomSource 時要使用的 StringCollection。
AutoCompleteMode:指示文本框的文本完成行為。
AutoCompleteSource:自動完成源,可以是 AutoCompleteSource 的枚舉值之一。
就行了, 一個簡單的示例如下
代碼如下:
textBox1.AutoCompleteCustomSource .AddRange(new string[] { "java","javascript","js","c#","c","c++" });
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
可是這種方式的不支持我們中文的簡拼自動完成(如在文本框裡輸入"gz"就會出現"廣州")。只好自己寫一個支持簡拼自動完成的控件了。
這是效果圖
控件不太復雜,一個TextBox和一個ListBox。代碼方面,用DataTable作數據源,每次在TextBox的值時,通過DataTable的Select方法,配上合適的表達式(如:{0} like '{1}%' and IsNull([{2}], ' ') <> ' ')來篩選出合適的備選文本內容,以下則是控件的代碼:
代碼如下:
private TextBox _tb;
private ListBox _lb;
private DataTable _dt_datasource;
private bool _text_lock;
private string _general_text;//原始輸入文本框的值
private bool _lb_kd_first_top;//listbox是否第一次到達頂部
private int _itemCount;
代碼如下:
/// <summary>
/// TextBox的Text屬性,增加了_text_lock操作,放置觸發TextChanged事件
/// </summary>
private string TextBoxText
{
get { return _tb.Text; }
set
{
_text_lock = true;
_tb.Text = value;
_text_lock = false;
}
}
/// <summary>
/// 顯示在ListBox的字段名
/// </summary>
public string ValueName { get; set; }
/// <summary>
/// 用於匹配的字段名
/// </summary>
public string CodeName { get; set; }
/// <summary>
/// 顯示提示項的數量
/// </summary>
public int ItemCount
{
get
{ return _itemCount; }
set
{
if (value <= 0)
_itemCount = 1;
else
_itemCount = value;
}
}
public DataTable DataSource
{
get { return _dt_datasource; }
set { _dt_datasource = value; }
}
代碼如下:
public AutoComplete()
{
InitialControls();
}
代碼如下:
void AutoComplete_Load(object sender, EventArgs e)
{
_tb.Width = this.Width;
_lb.Width = _tb.Width;
this.Height = _tb.Height-1;
}
void AutoComplete_LostFocus(object sender, EventArgs e)
{
_lb.Visible = false;
this.Height = _tb.Height-1;
}
代碼如下:
//列表框按鍵事件
void _lb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0 || !_lb.Visible) return;
if (!_lb_kd_first_top && ((e.KeyCode == Keys.Up && _lb.SelectedIndex == 0) || (e.KeyCode == Keys.Down && _lb.SelectedIndex == _lb.Items.Count)))
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_lb_kd_first_top = _lb.SelectedIndex != 0;
}
if (e.KeyCode == Keys.Enter && _lb.SelectedIndex != -1)
{
_lb.Visible = false;
this.Height = _tb.Height;
_tb.Focus();
}
}
//列表鼠標單擊事件
void _lb_Click(object sender, EventArgs e)
{
if (_lb.SelectedIndex != -1)
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
}
_lb.Visible = false;
_tb.Focus();
this.Height = _tb.Height;
}
代碼如下:
//文本框按鍵事件
void _tb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0||!_lb.Visible) return;
bool _is_set = false;
if (e.KeyCode == Keys.Up)
{
if (_lb.SelectedIndex <= 0)
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex--;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Down)
{
if (_lb.SelectedIndex == _lb.Items.Count - 1)
{
_lb.SelectedIndex = 0;
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex++;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Enter)
{
_lb.Visible = false;
this.Height = _tb.Height;
_is_set = _lb.SelectedIndex != -1;
}
_lb_kd_first_top = _lb.SelectedIndex != 0;
if (_is_set)
{
_text_lock = true;
_tb.Text = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_tb.SelectionStart = _tb.Text.Length + 10;
_tb.SelectionLength = 0;
_text_lock = false;
}
}
//文本框文本變更事件
void _tb_TextChanged(object sender, EventArgs e)
{
if (_text_lock) return;
_general_text = _tb.Text;
_lb.Visible = true;
_lb.Height = _lb.ItemHeight * (_itemCount+1);
this.BringToFront();
_lb.BringToFront();
this.Height = _tb.Height + _lb.Height;
DataTable temp_table = _dt_datasource.Clone();
string filtStr = FormatStr(_tb.Text);
DataRow [] rows = _dt_datasource.Select(string.Format(GetFilterStr(),CodeName,filtStr,_lb.DisplayMember));
for (int i = 0; i < rows.Length&&i<_itemCount; i++)
{
temp_table.Rows.Add(rows[i].ItemArray);
}
_lb.DataSource = temp_table;
if (_lb.Items.Count > 0) _lb.SelectedItem = _lb.Items[0];
}
代碼如下:
/// <summary>
/// 初始化控件
/// </summary>
private void InitialControls()
{
_lb_kd_first_top = true;
_tb = new TextBox();
_tb.Location = new Point(0, 0);
_tb.Margin = new System.Windows.Forms.Padding(0);
_tb.Width = this.Width;
_tb.TextChanged += new EventHandler(_tb_TextChanged);
_tb.KeyUp += new KeyEventHandler(_tb_KeyDown);
_lb = new ListBox();
_lb.Visible = false;
_lb.Width = _tb.Width;
_lb.Margin = new System.Windows.Forms.Padding(0);
_lb.DisplayMember = ValueName;
_lb.SelectionMode = SelectionMode.One;
_lb.Location = new Point(0, _tb.Height);
_lb.KeyUp += new KeyEventHandler(_lb_KeyDown);
_lb.Click += new EventHandler(_lb_Click);
this.Controls.Add(_tb);
this.Controls.Add(_lb);
this.Height = _tb.Height - 1;
this.LostFocus += new EventHandler(AutoComplete_LostFocus);
this.Leave += new EventHandler(AutoComplete_LostFocus);
this.Load += new EventHandler(AutoComplete_Load);
}
/// <summary>
/// 獲取過濾格式字符串
/// </summary>
/// <returns></returns>
private string GetFilterStr()
{
//未過濾注入的字符 ' ] %任意 *任意
string filter = " {0} like '{1}%' and IsNull([{2}], ' ') <> ' ' ";
if (_dt_datasource.Rows[0][CodeName].ToString().LastIndexOf(';') > -1)
filter = " {0} like '%;{1}%' and IsNull([{2}],' ') <> ' ' ";
return filter;
}
/// <summary>
/// 過濾字符串中一些可能造成出錯的字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string FormatStr(string str)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
str = str.Replace("[", "[[]").Replace("%", "[%]").Replace("*", "[*]").Replace("'", "''");
if (CodeName == "code") str = str.Replace(" ", "");
return str;
}
下面是使用控件的例子 代碼如下:
class Common
{
/// <summary>
/// 生成測試數據源
/// </summary>
public static DataTable CreateTestDataSoucre
{
get
{
List<KeyValuePair<string, string>> source = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("張三",";zs;張三;"),
new KeyValuePair<string,string>("李四",";li;李四;"),
new KeyValuePair<string,string>("王五",";ww;王五;"),
new KeyValuePair<string,string>("趙六",";zl;趙六;"),
new KeyValuePair<string,string>("洗刷",";cs;csharp;c#;洗刷;"),
new KeyValuePair<string,string>("爪哇",";java;爪哇;"),
new KeyValuePair<string,string>("java",";java;"),
new KeyValuePair<string,string>("c#",";c#;cs;csharp;"),
new KeyValuePair<string,string>("javascript",";javascript;js;")
};
DataTable table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");
table.Columns.Add("code");
for (int i = 0; i < source.Count; i++)
{
DataRow row = table.Rows.Add();
row["id"] = i;
row["name"] = source[i].Key;
row["code"] = source[i].Value;
}
return table;
}
}
}
//.............
AutoComplete ac=new AutoComplete();
ac.ValueName = "name";
ac.CodeName = "code";
ac.DataSource= Common.CreateTestDataSoucre;
ac.ItemCount= 5;