最近因项目中要用,于是就扩展了TextBox实现输入汉字首字母简单查询。控件扩展代码很简单。但是刚开始,一直没有找到扩展入口,始终无法展现下拉框。先想通过AutoCompleteCustomSource属性改变来实现,但最终没法扩展。后又想到ComboBox但是,也不好控制。经过一天的思考后决定用TextBox + ListBox组合扩展(主要为继承至TextBox和持有ListBo)。(文/破浪

效果先上:

附件: SpellSplit.jpg

最要实现代码有:

类原型
public class SpellSearchBoxEx : TextBox

{

private ListBox searchList;

.....

1:SpellSearchSource :查询数据源(汉字)

事件的绑定和选择全在属性里;
  1. public string[] SpellSearchSource

  2.         {

  3.             get { return source; }

  4.             set

  5.             {

  6.                 if (value != null)

  7.                 {

  8.                     source = value;

  9.                     InitSourceSpell();//实现数据转化为相应拼音并存储;

  10.                     TextChanged += new EventHandler(SpellSearchBoxEx_TextChanged);

  11.                     LostFocus += new EventHandler(SpellSearchBoxEx_LostFocus);

  12. // searchList是一个ListBox,它用来显示下拉框;

  13.                     searchList.KeyDown += new KeyEventHandler(searchList_KeyDown);

  14.                     searchList.Click += new EventHandler(searchList_Click);

  15.                     searchList.MouseMove += new MouseEventHandler(searchList_MouseMove);

  16.                 }



  17.             }

  18.         }
复制代码
2:TextChanged事件代码
  1. protected virtual void SpellSearchBoxEx_TextChanged(object sender, EventArgs e)

  2.         {

  3.             searchList.Items.Clear();

  4.             string str = this.Text;

  5. //输入的是汉字拼音情况;

  6.             foreach (string var in spellList)

  7.             {

  8.                 if (SearchMode == SearchMode.Contains)

  9. // SearchMode为枚举变量:StartsWith 和Contains

  10.                 {

  11.                     if (var.IndexOf(str.ToUpper()) != -1)

  12.                     {

  13. // spellList是先前存储转化拼音List<string>。

  14.                         searchList.Items.Add(source[spellList.IndexOf(var)]);

  15.                     }

  16.                 }

  17.                 else

  18.                 {

  19.                     if (var.ToUpper().StartsWith(str.ToUpper()))

  20.                     {

  21.                         searchList.Items.Add(source[spellList.IndexOf(var)]);

  22.                     }

  23.                 }

  24.             }

  25. //输入的是汉字情况;

  26.             if (Regex.IsMatch(str, "[\u4e00-\u9fa5]+"))

  27.             {



  28.                 foreach (string var in source)

  29.                 {

  30.                     if (SearchMode == SearchMode.Contains)

  31.                     {

  32.                         if (var.ToUpper().IndexOf(str.ToUpper()) != -1 && !searchList.Items.Contains(var))

  33.                         {

  34.                             searchList.Items.Add(var);

  35.                         }

  36.                     }

  37.                     else

  38.                     {

  39.                         if (var.ToUpper().StartsWith(str.ToUpper())&& !searchList.Items.Contains(var))

  40.                         {

  41.                             searchList.Items.Add(var);

  42.                         }

  43.                     }

  44.                 }



  45.             }

  46.             SetSearchBoxState();



  47.         }
复制代码
3:控制ListBox的显示和隐藏
  1.         private void SetSearchBoxState()

  2.         {

  3.             if (searchList.Items.Count > 0)

  4.             {

  5.                 searchList.BorderStyle = BorderStyle.FixedSingle;

  6. // maxItemCount为下拉框最大显示数;

  7.                 searchList.Height = ((searchList.Items.Count >= maxItemCount ? maxItemCount : searchList.Items.Count) + 1) * searchList.ItemHeight;

  8.                 searchList.Parent = this.Parent;

  9.                 searchList.Location = new System.Drawing.Point(this.Left, this.Bottom);

  10.                 searchList.Width = this.Width;

  11.                 searchList.BringToFront();

  12.                 searchList.Visible = true;

  13.             }

  14.             else

  15.             {

  16.                 searchList.Visible = false;

  17.             }

  18.         }
复制代码
4:剩下的为:控制鼠标滑动和键盘方向键操作,很简单,主要利用ListBox的选中项为高亮显示;及在textBox LostFocus 并非ListBox获得焦点时隐藏结果控件ListBox

5:

ListBox上回车或者是点击时提交代码
  1. protected virtual void CommitSearchList()

  2.         {

  3.             if (searchList.SelectedIndex > -1)

  4.             {

  5.                 this.Text = searchList.Items[searchList.SelectedIndex].ToString();

  6.                 searchList.Visible = false;

  7.                 this.Focus();

  8.             }

  9.         }
复制代码
6:主体汉字检索拼音转化代码,请在下面下载,很简单,可以直接复制拿来用。

代码下载:
TOP