Combobox控件實現漢字按拼音首字母檢索

Combobox控件在開發中作爲下拉選項的不二之選,用的非常頻繁,前幾日開發過程中剛好有個需求有用到這個控件,而且客戶要求增加下拉選擇功能,這個簡單,設置控件的自動完成屬性後就解決了

this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;//設置自動完成的源 
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;//設置自動完成的的形式

發現場讓客戶使用,客戶表示功能實現和他需求不一致,後來解釋是想通過下拉選項的拼音碼進行檢索,查遍的很多資料,也走了很多彎路,最後終於實現,將正確的實現方法記錄在此


首先根據Combobox控件拼音碼檢索搜索到了這篇文章(http://www.cnblogs.com/eshizhan/archive/2012/08/13/2637207.html),集成到項目中進行測試時發現,輸入檢索的內容後,敲擊回車鍵,輸入的內容沒有全部顯示到文本框中,輸入三個字母時,只顯示一個或者兩個,再次檢索時候恢復正常

如果檢索時,把鼠標指針從控件上移動到別的控件上,輸入檢索內容敲擊回車鍵後,會出現丟失鼠標指針的情況,這時只有把鼠標往下移動到任務欄指針纔會出現

以上問題測試均使用搜狗輸入法中文狀態


網上並沒有找到針對這兩個問題的解決方案,繼續搜索拼音碼檢索,查找不同的方案,經過對多個方案的反覆測試,終於發現了一個可用的方案,重寫了ComboBox控件,以下爲示例代碼

    public class ComboBoxEx : System.Windows.Forms.ComboBox
    {
        public ComboBoxEx()
        {
            this.FormattingEnabled = true;
        }
        List<ChineseCharacter> _ChineseCharacterList;
        private string[] _TextList;
        public string[] TextList
        {
            get { return _TextList; }
            set
            {
                _TextList = value;
                _ChineseCharacterList = GetChineseCharacterList(value);
            }
        }
        /// <summary>
        /// 把中文字符集合轉爲中文字符對象集合
        /// </summary>
        private List<ChineseCharacter> GetChineseCharacterList(string[] chnTextList)
        {
            List<ChineseCharacter> lst = new List<ChineseCharacter>();
            foreach (string s in chnTextList)
            {
                ChineseCharacter cc = new ChineseCharacter(s);
                lst.Add(cc);
            }
            return lst;
        }
        protected override void OnTextUpdate(EventArgs e)
        {
            if (_ChineseCharacterList != null && _ChineseCharacterList.Count > 0)
            {
                //var input = this.Text.Trim().ToUpper();
                string input = this.Text.Trim().ToUpper();
                this.Items.Clear();
                if (string.IsNullOrEmpty(input))
                {
                    this.Items.AddRange(_ChineseCharacterList.ToArray());
                }
                else
                {
                    var newList = _ChineseCharacterList.FindAll(c => c.FirstPinYin.Contains(input) || c.ChineseText.Contains(input));
                    if (newList.Count == 0)
                    {
                        newList.Add(new ChineseCharacter("未找到"));
                    }
                    this.Items.AddRange(newList.ToArray());
                }
                this.DisplayMember = "ChineseText";
                this.ValueMember = "FirstPinYin";
                this.Select(this.Text.Length, 0);
                this.DroppedDown = true;
                //保持鼠標指針形狀  
                Cursor = Cursors.Default;
            }
            base.OnTextUpdate(e);
        }
        protected override void OnEnter(EventArgs e)
        {
            this.Items.AddRange(_ChineseCharacterList.ToArray());
            this.DisplayMember = "ChineseText";
            this.ValueMember = "FirstPinYin";
            base.OnEnter(e);
        }
        protected override void OnLeave(EventArgs e)
        {
            this.Items.Clear();
            base.OnLeave(e);
        }
    }
    /// <summary>
    /// 中文字符
    /// </summary>
    public class ChineseCharacter
    {
        public ChineseCharacter(string chnText)
        {
            ChineseText = chnText;
            FirstPinYin =JHNISCommonLib.JHNISCommonManage.GetInstance().JianPin(chnText);
        }
        public string ChineseText { get; set; }
        public string FirstPinYin { get; set; }
    }


第二種方案,使用搜索輸入法輸入漢字,敲擊回車鍵後,輸入的內容沒有全部顯示到Combobox控件上,如下圖所示(輸入“瀏覽器”,只顯示了“覽器”):

wKioL1gYWijAW9ULAAAVayZS6b4817.png

經測試當把下拉列表顯示出來再進行檢索時,內容顯示正常,可通過以下代碼進行設置

        protected override void OnGotFocus(EventArgs e)
        {
            this.DroppedDown = true;
            base.OnGotFocus(e);
        }
        protected override void OnLostFocus(EventArgs e)
        {
            this.DroppedDown = false;
            base.OnLostFocus(e);
        }



第二種方案原文地址:http://download.csdn.net/detail/xbl002/9408842#comment

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章