天天看點

支援拼音檢索的TextBox擴充控件

最近因項目中要用,于是就擴充了TextBox實作輸入漢字首字母簡單查詢。控件擴充代碼很簡單。但是剛開始,一直沒有找到擴充入口,始終無法展現下拉框。先想通過AutoCompleteCustomSource屬性改變來實作,但最終沒法擴充。後又想到ComboBox但是,也不好控制。經過一天的思考後決定用TextBox + ListBox組合擴充(主要為繼承至TextBox和持有ListBox;

效果先上:

最要實作代碼有:

類原型public class SpellSearchBoxEx : TextBox

{

private ListBox searchList;

.....

1:

SpellSearchSource :查詢資料源(漢字)

事件的綁定和選擇全在屬性裡;

public string[] SpellSearchSource

        {

            get { return source; }

            set

            {

                if (value != null)

                {

                    source = value;

                    InitSourceSpell();//實作資料轉化為相應拼音并存儲;

                    TextChanged += new EventHandler(SpellSearchBoxEx_TextChanged);

                    LostFocus += new EventHandler(SpellSearchBoxEx_LostFocus);

// searchList是一個ListBox,它用來顯示下拉框;

                    searchList.KeyDown += new KeyEventHandler(searchList_KeyDown);

                    searchList.Click += new EventHandler(searchList_Click);

                    searchList.MouseMove += new MouseEventHandler(searchList_MouseMove);

                }

            }

        }

2:TextChanged事件代碼

 protected virtual void SpellSearchBoxEx_TextChanged(object sender, EventArgs e)

        {

            searchList.Items.Clear();

            string str = this.Text;

            foreach (string var in spellList)

            {

                if (SearchMode == SearchMode.Contains)

                {

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

                    {

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

                    }

                }

                else

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

                        for (int i = 0; i < spellList.Count; i++)

                        {

                            if (spellList[i].Equals(var) && !searchList.Items.Contains(source[i]))

                            {

                                searchList.Items.Add(source[i]);

                            }

                        }

            }

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

                foreach (string var in source)

                    if (SearchMode == SearchMode.Contains)

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

                            searchList.Items.Add(var);

                    else

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

            SetSearchBoxState();

        }

3:控制ListBox的顯示和隐藏

        private void SetSearchBoxState()

            if (searchList.Items.Count > 0)

                searchList.BorderStyle = BorderStyle.FixedSingle;

// maxItemCount為下拉框最大顯示數;

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

                searchList.Parent = this.Parent;

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

                searchList.Width = this.Width;

                searchList.BringToFront();

                searchList.Visible = true;

            else

                searchList.Visible = false;

4:剩下的為:控制滑鼠滑動和鍵盤方向鍵操作,很簡單,主要利用ListBox的選中項為高亮顯示;及在textBox LostFocus 并非ListBox獲得焦點時隐藏結果控件ListBox

5:

ListBox上回車或者是點選時送出代碼

protected virtual void CommitSearchList()

            if (searchList.SelectedIndex > -1)

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

                this.Focus();

6:主體漢字檢索拼音轉化代碼,見下:很簡單,可以直接複制拿來用。

代碼

本文轉自破狼部落格園部落格,原文連結:http://www.cnblogs.com/whitewolf/archive/2009/12/03/1615975.html,如需轉載請自行聯系原作者